这段时间做了一个小项目:经客户要求,在网上找到了一些相关资源,整理了一下
和大家分享一下:
代码:
package com.hdvon.witlv.util;
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;
/**
* @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实现文字上下滚动这个功能,我目前有两种方法实现: 一个是在TextView 中加上翻转的动画效果,然后设置循环滚动;一种是改写ViewPager 的滚动方向,使它从下到上进行滚动,并设置循环滚动; 首先介绍第...
一个纵向滑动的也就是上下滑动页面的效果,分享给大家
这是一个关于TextView上下滚动的效果,详情请前往博客浏览http://blog.csdn.net/u014452224/article/details/53101507
这是一款可以上下左右滑动的android控件,和前端的table tr、td、差不多,全部源码,可以自己重新定义
一个简单的文字上下滚动,从项目中抽取出来的。
Android 文字跑马灯和上下翻滚效果
博客地址:http://blog.csdn.net/wa6392039/article/details/53103746
内部有两个比较有价值的东西,一个是,文字过长时,自动的滚动显示组件;另一个是优雅的滑动组件。类似于gallery的侧滑效果。
主要为大家详细介绍了Android仿抖音上下滑动布局,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Android手势识别——上下左右滑动、屏幕上下左右中区域处理
有些应用里面会有一些上下滚动文字的广告,尤其是关于商城里面的容易出现,具体的效果相比大家也见过,TextSwitche
Android:TextView的垂直滚动效果和上下滚动效果,原生动画实现
主要介绍了Android实现在TextView文字过长时省略部分或滚动显示的方法,结合实例形式分析了Android中TextView控件文字显示及滚动效果相关操作技巧,需要的朋友可以参考下
Android实现TextView文字滚动
android ScrollView上下左右任意滑动,图片原样超出屏幕尺寸任意滑动。
MyPickerView Watch the GIF please
仿映客viewPager上下滑动切换直播demo 效果预览 APK地址 InkeVerticalViewPagerLive-sample.apk 简述 经过几天对映客APP的分析研究,明白了映客上下滑动切换新的直播的实现原理,真心觉得做的挺赞的。。还是很流畅...
通过重写ScrollView和HorizontalScrollView实现任意布局或者大图片、图表等的上下左右滑动,支持同时上下左右滑动,解决了原先滑动卡顿和同一时间只能一个方向滑动的问题~