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