按钮单击时增加和减少自动滚动文本速度

时间:2017-04-22 11:55:41

标签: java android autoscroll

我试图在按钮点击时实现自动滚动文本的速度的增加和减少,但我无法弄清楚如何做到这一点。我试图声明默认速度并使用按钮增加和减少其值,但这不起作用。 任何帮助都会很棒。请帮助我

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;
}

1 个答案:

答案 0 :(得分:2)

看看这个Marquee Set Speed

如果您想提高滚动速度,则 减少 的值为:

  

private int mRndDuration = 10000; //减少mRndDuration的值以提高滚动速度