欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现字幕滚动的方法

程序员文章站 2023-02-02 12:20:39
本文实例介绍了安卓android实现字幕滚动效果的方法。主要是一个现成的java类文件实现的,该程序由android达人tony编写,本次是转发,还望原作者tony不要介意...

本文实例介绍了安卓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;
/**
 * @author tony
 */
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;
 /**
 * @param context
 */
 public autoscrollview(context context) {
 this(context, null);
 }
 /**
 * @param context
 * @param attrs
 */
 public autoscrollview(context context, attributeset attrs) {
 this(context, attrs, 0);
 }
 /**
 * @param context
 * @param attrs
 * @param defstyle
 */
 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; 
   } 
 /**
 * 判断当前是否为滚动状态
 * @return the isscrolled
 */
 public boolean isscrolled() {
 return isscrolled;
 }
 /**
 * 开启或者关闭自动滚动功能
 * @param isscrolled
 *      true为开启,false为关闭
 */
 public void setscrolled(boolean isscrolled) {
 this.isscrolled = isscrolled;
 autoscroll();
 }
 /**
 * 获取当前滚动到结尾时的停顿时间,单位:毫秒
 * @return the period
 */
 public long getperiod() {
 return period;
 }
 /**
 * 设置当前滚动到结尾时的停顿时间,单位:毫秒
 * @param period
 *the period to set
 */
 public void setperiod(long period) {
 this.period = period;
 }
 /**
 * 获取当前的滚动速度,单位:毫秒,值越小,速度越快。
 * @return the speed
 */
 public long getspeed() {
 return duration;
 }
 /**
 * 设置当前的滚动速度,单位:毫秒,值越小,速度越快。
 * @param speed
 *the duration to set
 */
 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);
 }
}