大家可能在网上看到许多中奖结果滚动效果,这种效果怎么实现呢?
public class AutoScrollView extends ScrollView { private long speed = 50; private int eachRollLength = 1; private Timer timer; private TimerTask timerTask; private boolean isCanScroll = false; private boolean scrollEnable = false;//是否可以触摸滚动 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); } @Override public boolean onInterceptTouchEvent(MotionEvent p_event) { return scrollEnable; } /** * 开始滚动 */ public void startRoll() { // 取消之前的滚动 cancelRoll(); smoothScrollTo(0, 0);// 滚动到开始位置 timer = new Timer(); timerTask = new TimerTask() { @Override public void run() { if (!isCanScroll) return; if (getScrollY() >= getHeight()) { // 超出滚动列表 smoothScrollTo(0, 0); } else { smoothScrollBy(0, eachRollLength); } } }; isCanScroll = true; timer.schedule(timerTask, speed, speed); } /** * 暂停滚动 */ public void pauseRoll() { isCanScroll = false; } /** * 重新恢复滚动 */ public void restartRoll() { isCanScroll = true; } /** * 停止滚动,即取消滚动 */ public void stopRoll() { cancelRoll(); } /** * 取消滚动 */ public void cancelRoll() { if (timerTask != null) { timerTask.cancel(); timerTask = null; } if (timer != null) { timer.cancel(); timer = null; } } /** * 设置是否可以触摸滚动 */ public void setScrollEnable(boolean scrollEnable) { this.scrollEnable = scrollEnable; } /** * 设置每次滚动的长度 */ public void setEachRollLength(int eachRollLength) { this.eachRollLength = eachRollLength; } /** * 设置滚动的速度,值越大,速度越小 */ public void setSpeed(long speed) { this.speed = speed; } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.qiyuan.jindongshangcheng.view.AutoScrollView android:id="@+id/autoscrollview" android:layout_width="match_parent" android:layout_height="100dp"> <LinearLayout android:id="@+id/containt" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> </com.qiyuan.jindongshangcheng.view.AutoScrollView> </RelativeLayout>注意,外面必须嵌套一个控件,不然在Android6.0以上系统,不会自己滚动
在MainActivity中使用
package com.qiyuan.jindongshangcheng; import android.app.Activity; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; import com.qiyuan.jindongshangcheng.view.AutoScrollView; /** * Created by huanghaojie on 2016/10/8. */ public class MainActivity5 extends Activity { private LinearLayout contain; private AutoScrollView autoScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mian5); contain = (LinearLayout) findViewById(R.id.containt); autoScrollView = (AutoScrollView) findViewById(R.id.autoscrollview); initView(); } private void initView(){ contain.removeAllViews(); for (int i = 0; i <50 ; i++) { TextView te = new TextView(this); te.setText(i+""); contain.addView(te); }
autoScrollView.startRoll();
} }