Android中獎結果滾動效果

大家可能在網上看到許多中獎結果滾動效果,這種效果怎麼實現呢?

下面是一個可以觸摸滾動的自定義View

package com.qiyuan.jindongshangcheng.view;

/**
 * Created by huanghaojie on 2016/10/8.
 */

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 true;
    }
    @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);
    }
}
2.下面是不可觸摸滾動的自定義View

package com.qiyuan.jindongshangcheng.view;

/**
 * Created by huanghaojie on 2016/10/8.
 */

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 false;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent p_event)
    {
        Log.d("test", "onInterceptTouchEvent");
        return false;
    }
    /**
     * 判斷當前是否爲滾動狀態
     * @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);
    }
}

佈局文件

<?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);
        autoScrollView.setScrolled(true);

        initView();

    }
    private void initView(){
        contain.removeAllViews();
        for (int i = 0; i <50 ; i++) {
            TextView te = new TextView(this);
            te.setText(i+"");
            contain.addView(te);
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章