android 获取滑动速率

public class MainActivity extends FragmentActivity implements View.OnTouchListener {

    private ListView listView = null;
    private MyView myViewGroup;
    private LinearLayout main;

    private VelocityTracker mVelocityTracker = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        myViewGroup = (MyView) findViewById(R.id.myviewGroup);
        main = (LinearLayout) findViewById(R.id.main);
        main.setOnTouchListener(this);
    }

    public void scroll(View view) {

        myViewGroup.beginScroll();

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("xz", "ACTION_DOWN");
                break;

            case MotionEvent.ACTION_MOVE:// 移动的时候
                Log.i("xz", "ACTION_MOVE");
                break;

            case MotionEvent.ACTION_UP:
               //1000是1000毫秒的意思,即获取每秒的速率
                mVelocityTracker.computeCurrentVelocity(1000);
                Log.i("xz", "ACTION_UP");
                //0是初始数据
                Log.i("xz", mVelocityTracker.getXVelocity(0) + "");
                Log.i("xz", mVelocityTracker.getYVelocity(0) + "");
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //用完记得回收
        mVelocityTracker.recycle();
        mVelocityTracker = null;
    }
}

你可能感兴趣的:(android 获取滑动速率)