Android 自定義View 使用VelocityTracker記錄滑動速度

1.簡介

VelocityTracker是android提供的用來記錄滑動速度的一個類,可以監控手指移動的速度。

 

 

2.VelocityTracker常用方法

 

2.1.

方法:public static VelocityTracker obtain()

作用:獲取一個新的VelocityTracker對象。

 

2.2.

方法:public void recycle()

作用:返回一個VelocityTracker對象以供其他人重用。

 

2.3.

方法:public void addMovement(MotionEvent event)

作用:向跟蹤器添加用戶的移動。

 

2.4.

方法:public float getXVelocity()

作用:檢索最後計算的X速度。

 

2.5.

方法:public float getYVelocity()

作用:檢索最後計算的Y速度。

 

2.6.

方法:public void clear()

作用:將速度跟蹤器重置爲初始狀態。

 

2.7.

方法:public void computeCurrentVelocity(int units)

作用:計算過去unitsms內的速度。這個速度其實就是 ((當前的位置)-(之前的位置))/時間.如果得到的值爲200            就表示這1000ms內,X方向移動了200像素。速度是有正負的,右劃就是正的,左劃就是負的,上劃爲負,          下劃爲正。

 

 

2.8.

方法:public void computeCurrentVelocity(int units, float maxVelocity)

作用:計算過去unitsms內的速度。maxVelocity:最大速度,實際上是一個上限,比如我們當前速度300,但是上限爲200,那用getXVelocity()得到的值就是200,不可能超過上限(無論正負)。

 

 

 

 

 

3.配合onTouch使用

 

佈局

<?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">

<TextView
    android:id="@+id/activity_motionevent_textview"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:clickable="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:background="#FF0000"
    android:textColor="#FFFFFF"
    android:text="Android 自定義View之MotionEvent方法詳解"/>

</RelativeLayout>

 

 

 

Java代碼

public class MotionEventActivity extends AppCompatActivity {

    private TextView textView;
    private int lastX;
    private int lastY;
    private VelocityTracker velocityTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motionevent);

        if(velocityTracker == null) {//velocityTracker對象爲空 獲取velocityTracker對象
            velocityTracker = VelocityTracker.obtain();
        }else {//velocityTracker對象不爲空 將velocityTracker對象重置爲初始狀態
            velocityTracker.clear();
        }

        textView= (TextView) findViewById(R.id.activity_motionevent_textview);
        textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                int action=motionEvent.getAction();
                int x= (int) motionEvent.getRawX();
                int y= (int) motionEvent.getRawY();
                velocityTracker.addMovement(motionEvent);//向velocityTracker對象添加action
                switch(action){
                    case MotionEvent.ACTION_DOWN://按下
                        lastX=x;
                        lastY=y;
                        break;
                    case MotionEvent.ACTION_MOVE://移動
                        velocityTracker.computeCurrentVelocity(1000);
                        Log.d("TAG", "移動X velocity: " + velocityTracker.getXVelocity());
                        Log.d("TAG", "移動Y velocity: " + velocityTracker.getYVelocity());
                        break;
                    case MotionEvent.ACTION_UP://擡起
                        velocityTracker.computeCurrentVelocity(1000);
                        Log.d("TAG", "擡起X velocity: " + velocityTracker.getXVelocity());
                        Log.d("TAG", "擡起Y velocity: " + velocityTracker.getYVelocity());
                        break;
                }
                return false;
            }
        });
    }

    /**
     * onDestroy方法
     * */

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (velocityTracker != null) {
            velocityTracker.recycle();
            velocityTracker = null;
        }
    }
}

 

效果:

 

 

 

 

 

 

附:VelocityTracker官方鏈接

https://developer.android.google.cn/reference/android/view/VelocityTracker

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章