本文实例介绍了安卓Android实现字幕滚动效果的方法。主要是一个现成的Java类文件实现的,该程序由Android达人Tony编写,本次是转发,还望原作者Tony不要介意。这个Android字幕滚动类的自定义功能比较多,可定义当前滚动到结尾时的停顿时间,单位:毫秒,还可设置当前的滚动速度,值越小,速度越快。
主要实现代码如下:
package com.tony.autoscroll;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;
public class AutoScrollView extends ScrollView {
private final Handler handler = new Handler();
private long duration = 50;
private boolean isScrolled = false;
private int currentIndex = 0;
private long period = 1000;
private int currentY = -1;
private double x;
private double y;
private int type = -1;
public AutoScrollView(Context context) {
this(context, null);
}
public AutoScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public AutoScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public boolean onTouchEvent(MotionEvent event) {
int Action = event.getAction();
switch (Action) {
case MotionEvent.ACTION_DOWN:
x=event.getX();
y=event.getY();
if (type == 0) {
setScrolled(false);
}
break;
case MotionEvent.ACTION_MOVE:
double moveY = event.getY() - y;
double moveX = event.getX() - x;
Log.d("test", "moveY = " + moveY + " moveX = " + moveX );
if ((moveY>20||moveY<-20) && (moveX < 50 || moveX > -50) && getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
if (type == 0) {
currentIndex = getScrollY();
setScrolled(true);
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent p_event)
{
Log.d("test", "onInterceptTouchEvent");
return true;
}
public boolean isScrolled() {
return isScrolled;
}
public void setScrolled(boolean isScrolled) {
this.isScrolled = isScrolled;
autoScroll();
}
public long getPeriod() {
return period;
}
public void setPeriod(long period) {
this.period = period;
}
public long getSpeed() {
return duration;
}
public void setSpeed(long speed) {
this.duration = speed;
}
public void setType(int type){
this.type = type;
}
private void autoScroll() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
boolean flag = isScrolled;
if (flag) {
//Log.d("test", "currentY = " + currentY + " getScrollY() = "+ getScrollY() );
if (currentY == getScrollY()) {
try {
Thread.sleep(period);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentIndex = 0;
scrollTo(0, 0);
handler.postDelayed(this, period);
} else {
currentY = getScrollY();
handler.postDelayed(this, duration);
currentIndex++;
scrollTo(0, currentIndex * 1);
}
} else {
//currentIndex = 0;
//scrollTo(0, 0);
}
}
}, duration);
}
}
您可能感兴趣的文章:android开发之横向滚动/竖向滚动的ListView(固定列头)android实现上下滚动的TextViewandroid TextView不用ScrollViewe也可以滚动的方法android 实现ScrollView自动滚动的实例代码Android中实现多行、水平滚动的分页的Gridview实例源码Android GridView实现滚动到指定位置的方法android开发教程之文本框加滚动条scrollviewandroid ListView自动滚动方法android listview 水平滚动和垂直滚动的小例子android实现歌词自动滚动效果