0

I am trying to implement an increase and decrease of the speed of auto-scrolling text on button click, but I cannot figure out how to do that. I have tried to declare a default speed and to increment and decrement its value by using a button, but that didn't work. Any help would be great.please help me

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;
}
Dev
  • 75
  • 2
  • 11

1 Answers1

2

take a look at this Marquee Set Speed

If you want to increase the scrolling speed then reduce the value of :

private int mRndDuration = 10000;//reduce the value of mRndDuration to increase scrolling speed

Community
  • 1
  • 1
Ritesh Bhavsar
  • 1,343
  • 1
  • 9
  • 19
  • I have seen this.....but its scrolling horizontally.I need vertical scroll.Setting its Horizontalscroll false also didn't worked – Dev May 16 '17 at 11:40
  • 1
    Solved.. https://github.com/ritesh-bhavsar86/StockAutoScroll .. on button click tvcontent.setspeed(tvcontent.getspeed()-); – Ritesh Bhavsar May 17 '17 at 05:37