android实现字体滚动,Android实现字幕滚动的方法

现在制作的很多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 -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实现字幕滚动的方法,大家学会了吗?爱站技术频道将继续为大家搜集更多是知识,请大家持续关注我们的更新。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值