前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VelocityTracker的用法

VelocityTracker的用法

作者头像
提莫队长
发布2019-03-01 09:43:22
8980
发布2019-03-01 09:43:22
举报
文章被收录于专栏:刘晓杰刘晓杰

VelocityTracker在API中解释如下:

Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use obtain() to retrieve a new instance of the class when you are going to begin tracking, put the motion events you receive into it with addMovement(MotionEvent), and when you want to determine the velocity call computeCurrentVelocity(int) and then getXVelocity() and getYVelocity().  

方法如下:

简单的Demo(我已经把网上的demo代码竟可能的简化了,这样看起来清晰一些)

代码语言:javascript
复制
package com.example.velocitytrackertest;

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

public class MainActivity extends Activity {
	private VelocityTracker velocityTracker;

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

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//必须放这里,放在ACTION_DOWN里面XY输出为0
		if(velocityTracker == null){
			velocityTracker = VelocityTracker.obtain();//必须和recycle()配对
		}
		velocityTracker.addMovement(event);
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_MOVE:
			velocityTracker.computeCurrentVelocity(1000);
			int X = (int)velocityTracker.getXVelocity();
			int Y = (int)velocityTracker.getYVelocity();
			Log.i("X", X + "");
			Log.i("Y", Y + "");
			break;
		case MotionEvent.ACTION_UP:
			if(velocityTracker != null){
				velocityTracker.recycle();
				velocityTracker = null;
			}
			break;
		}
		return super.onTouchEvent(event);
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年03月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档