2017-04-22 75 views
0

我想實現增加和減少按鈕點擊自動滾動文本的速度,但我無法弄清楚如何做到這一點。我曾嘗試聲明一個默認速度,並通過使用按鈕增加或減少其值,但這並不奏效。 任何幫助將是great.please幫我增加和減少按鈕點擊自動滾動文本速度

ScrollTextView.java:

public class ScrollTextView extends TextView { 

public float DEFAULT_SPEED = 9.0f; 

public Scroller scroller; 
public float speed = DEFAULT_SPEED; 
public boolean continuousScrolling = true; 

public ScrollTextView(Context context) { 
    super(context); 
    scrollerInstance(context); 
} 

public ScrollTextView(Context context, AttributeSet attributes) { 
    super(context, attributes); 
    scrollerInstance(context); 
} 

public void scrollerInstance(Context context) { 
    scroller = new Scroller(context, new LinearInterpolator()); 
    setScroller(scroller); 
} 


@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    super.onLayout(changed, left, top, right, bottom); 
    if (scroller.isFinished()) { 
     scroll(); 
    } 
} 

public void scroll() { 
    int viewHeight = getHeight(); 
    int visibleHeight = viewHeight - getPaddingBottom() - getPaddingTop(); 
    int lineHeight = getLineHeight(); 

    int offset = -1 * visibleHeight; 
    int distance = visibleHeight + getLineCount() * lineHeight; 
    int duration = (int) (distance * speed); 


    scroller.startScroll(0, offset, 0, distance, duration); 
} 



public void setSpeed(float speed) { 
    this.speed = speed; 
} 

public float getSpeed() { 
    return speed; 
} 

public void setContinuousScrolling(boolean continuousScrolling) { 
    this.continuousScrolling = continuousScrolling; 
} 

public boolean isContinuousScrolling() { 
    return continuousScrolling; 
} 
+0

有人請幫助......因爲我已經看到了這個問題的解決 – Dev

回答

2

看看這個Marquee Set Speed

如果你想增加滾動速度然後減少的值爲:

私人INT mRndDuration = 10000; //減少mRndDuration的價值增加滾動速度

+0

.....但它的滾動horizo​​ntally.I需要垂直滾動。設置其Horizo​​ntalscroll false也沒有工作 – Dev

+1

解決.. https://github.com/ritesh-bhavsar86/StockAutoScroll ..在按鈕上單擊tvcontent.setspeed(tvcontent.getspeed() - <您的變量>); –