VelocityTracker追蹤手指滑動(dòng)的速度

如何在View中追蹤手指的滑動(dòng)速度呢?

關(guān)鍵類(lèi):VelocityTracker
思路:在VIew的onTouchEvent()中,當(dāng)MotionEvent.ACTION_DOWN時(shí)初始化VelocityTracker, 在MotionEvent.ACTION_MOVE進(jìn)行追蹤,當(dāng)滑動(dòng)停止之后(MotionEvent.ACTION_UP or MotionEvent.ACTION_CANCEL)不要忘記調(diào)用clear()來(lái)回收內(nèi)存.

寫(xiě)了一個(gè)代碼關(guān)于追蹤手指滑動(dòng)的速度。
用了兩種方式來(lái)顯示獲取的值:一種是直接將追蹤到的數(shù)值畫(huà)在了view上;一種是寫(xiě)了一個(gè)回調(diào)的接口。


/**
 * Created by Administrator on 2016/11/16 0016.
 */
public class TestVelocityView extends View {

    //用于回調(diào)的接口
    GetVelocityListener listener;
    //追蹤速度關(guān)鍵的類(lèi)。沒(méi)有這個(gè)這篇文章將毫無(wú)意義
    VelocityTracker velocityTracker;
    //要畫(huà)文字或者任何東西都需要的paint
    Paint paint = new Paint();

    public GetVelocityListener getListener() {
        return listener;
    }

    public void setListener(GetVelocityListener listener) {
        this.listener = listener;
    }

    public TestVelocityView(Context context) {
        this(context,null);
    }

    public TestVelocityView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public TestVelocityView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint.setTextSize(50);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //畫(huà)文字的代碼。
        canvas.save();
        paint.setColor(Color.BLACK);
        canvas.drawText("x = "+xVelocity+"y ="+yVelocity,getLeft(),getTop(),paint);
      //畫(huà)完之后回收一下
        canvas.restore();
    }

    int xVelocity = 0;
    int yVelocity = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //初始化
                velocityTracker = VelocityTracker.obtain();
                break;
            case MotionEvent.ACTION_MOVE:
              //追蹤
                velocityTracker.addMovement(event);
                velocityTracker.computeCurrentVelocity(1000);
                xVelocity = (int) velocityTracker.getXVelocity();
                yVelocity = (int) velocityTracker.getYVelocity();

                if (listener != null) {
                    listener.get(xVelocity, yVelocity);
                    //強(qiáng)制刷新一下view,否則不會(huì)一直掉onDraw。
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                //回收
                velocityTracker.clear();
                velocityTracker.recycle();
                break;
        }
        return true;
    }

    public interface GetVelocityListener {
        public void get(int x, int y);
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 什么是View View 是 Android 中所有控件的基類(lèi)。 View的位置參數(shù) View 的位置由它的四個(gè)頂...
    acc8226閱讀 1,409評(píng)論 0 7
  • layout: postdate: 2016-01-08title: Android開(kāi)發(fā)藝術(shù)探索-第三章-View...
    KuTear閱讀 1,974評(píng)論 2 18
  • ViewDragHelper實(shí)例的創(chuàng)建 ViewDragHelper重載了兩個(gè)create()靜態(tài)方法public...
    傀儡世界閱讀 740評(píng)論 0 3
  • CSS3 多列 CSS3 可以將文本內(nèi)容設(shè)計(jì)成像報(bào)紙一樣的多列布局,如下實(shí)例: 瀏覽器支持 表格中的數(shù)字表示支持該...
    鹿守心畔光閱讀 241評(píng)論 0 1
  • 在最近越來(lái)越想回到四川,在來(lái)到天津的第三年,真的不知道怎么過(guò)來(lái)的。痛苦了大一,勉強(qiáng)過(guò)了大二,大三該思考自己...
    夢(mèng)所到達(dá)的地方閱讀 286評(píng)論 0 0

友情鏈接更多精彩內(nèi)容