国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現字幕滾動的方法

2020-02-21 17:27:29
字體:
來源:轉載
供稿:網友

現在制作的很多APP應用程序都有字幕滾動這個效果,這個效果看起來很簡單,但是實現起來卻不是我們想象的那么容易,今天武林技術頻道小編分享給大家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); }}

本文是武林技術頻道小編為大家帶來的Android實現字幕滾動的方法,大家學會了嗎?武林技術頻道將繼續為大家搜集更多是知識,請大家持續關注我們的更新。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鹤山市| 黑山县| 弥勒县| 上高县| 东辽县| 鄂伦春自治旗| 上犹县| 繁峙县| 怀柔区| 石家庄市| 儋州市| 邢台县| 屏边| 迁安市| 新乐市| 抚宁县| 武安市| 江安县| 罗甸县| 临夏市| 定西市| 伊宁县| 上饶市| 松江区| 基隆市| 新民市| 彭州市| 项城市| 延寿县| 调兵山市| 阳原县| 泸定县| 黄平县| 玉林市| 望江县| 老河口市| 彰武县| 吉林市| 斗六市| 皋兰县| 诸城市|