关闭

Android 速度检测demo

标签: android
646人阅读 评论(0) 收藏 举报
分类:
package app.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;

public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    private VelocityTracker vTracker = null;
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                if(vTracker == null) {
                    vTracker = VelocityTracker.obtain();
                }
                else {
                    vTracker.clear();
                }
                vTracker.addMovement(event);
                break;
            case MotionEvent.ACTION_MOVE:
                vTracker.addMovement(event);
                vTracker.computeCurrentVelocity(1000);
                Log.v("", "X velocity is " + vTracker.getXVelocity() +" pixels per second");
                Log.v("", "Y velocity is " + vTracker.getYVelocity() +" pixels per second");
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                vTracker.recycle();
                break;
        }
        return true;
    }
}


vTracker.computeCurrentVelocity(1000)是设置计量单位,但是是毫秒。

vTracker.getXVelocity()

vTracker.getYVelocity()

得出来的值是多少像素每秒.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:138413次
    • 积分:2548
    • 等级:
    • 排名:第15042名
    • 原创:114篇
    • 转载:10篇
    • 译文:0篇
    • 评论:32条
    最新评论