android中測量手速的地方為數(shù)不少疾就,那么怎么獲得呢?api中提供了VelocityTracker類來追蹤我們在屏幕上的滑動速度强窖。我這里定義了一個自定義view泪姨,在自定義view中的onTouchEvent中游沿,獲得了一個 VelocityTracker的實例。在測量速度時肮砾,官方建議只使用一個實例
if(velocityTracker==null){
velocityTracker=VelocityTracker.obtain();
}
當我們獲得了追蹤手速的實例后诀黍,就可以將觸摸事件添加到實例velocityTracker中,系統(tǒng)會為我們計算仗处。但是在添加之后眯勾,計算之前,我們需要設置一個時間段疆柔,用來告訴系統(tǒng)咒精,我們需要獲取的是多長時間段內(nèi)的速度。
velocityTracker.addMovement(event);
velocityTracker.computeCurrentVelocity(1000);
在設置時間段時旷档,使用的單位為ms毫秒模叙。接下來就可以分別獲取x坐標軸上,y坐標軸上的速度了
float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();
官方建議我們在不使用手速追蹤velocityTracker 的時候鞋屈,重置回收該類范咨,因此有了下面的代碼:
public void release(){
if(velocityTracker!=null){
velocityTracker.clear();
velocityTracker.recycle();
velocityTracker=null;
}
}
下面放送這個自定義view的全部代碼:
public class VeloctiyView extends View{
VelocityTracker velocityTracker;
public VeloctiyView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
// setClickable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(velocityTracker==null){
velocityTracker=VelocityTracker.obtain();
}
velocityTracker.addMovement(event);
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
Log.e("Vel","ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
Log.e("Vel","action_up");
release();
break;
case MotionEvent.ACTION_MOVE:
Log.e("Vel","ACTION_MOVE");
velocityTracker.computeCurrentVelocity(1000);
float xVelocity = velocityTracker.getXVelocity();
float yVelocity = velocityTracker.getYVelocity();
Log.e("Vel",String.format("velx--->%f,vely--->%f",xVelocity,yVelocity));
break;
case MotionEvent.ACTION_CANCEL:
release();
break;
}
// 如果這里使用了super.onTouchEvent(event),那么就只會觸發(fā)action_down厂庇。因為事件不會往下面?zhèn)鬟f渠啊,解決辦法,返回true权旷,或者調(diào)用setClickable(true),
//實際上二者的效果是一樣的替蛉。
return true;
}
public void release(){
if(velocityTracker!=null){
velocityTracker.clear();
velocityTracker.recycle();
velocityTracker=null;
}
}
}
在activity中通過代碼的形式創(chuàng)建自定義view,并加載到視圖中
VeloctiyView veloctiyView=new VeloctiyView(this);
veloctiyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
veloctiyView.setBackgroundColor(Color.BLUE);
setContentView(veloctiyView);
打印效果如下:
05-09 00:07:04.988 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_DOWN
05-09 00:07:05.214 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.215 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->1938.670532,vely--->855.740784
05-09 00:07:05.230 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.230 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->3354.457764,vely--->2245.788818
05-09 00:07:05.247 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.247 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->2818.437500,vely--->2003.496094
05-09 00:07:05.266 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
05-09 00:07:05.266 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: velx--->2408.752930,vely--->1751.607788
05-09 00:07:05.281 7580-7580/com.xiaguangcheng.bindsconnectionpooldemo E/Vel: ACTION_MOVE
我們也可以使用GestureDetector來獲得速度拄氯,同樣是將觸摸事件交給GestureDetector類來處理躲查,我們只需要在回調(diào)中獲得就可以了
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
GestureDetector gestureDetector=new GestureDetector(getContext(), new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.e("VELO","velx--->"+velocityX+";velY--->"+velocityY);
return false;
}
});
打印效果如下:
05-09 01:22:02.864 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->9634.49;velY--->-5575.902
05-09 01:22:07.231 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->-13790.847;velY--->14729.759
05-09 01:22:10.449 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->359.31744;velY--->-235.8771
05-09 01:22:14.373 11115-11115/com.xiaguangcheng.bindsconnectionpooldemo E/VELO: velx--->-7355.338;velY--->-3538.2659