概述
我們都知道安卓手機的事件分爲兩類,一類是按鍵事件,另一類就是屏幕滑動事件,而我們大部分的事件都是通過屏幕滑動來產生的。在滑動的過程中你有沒有想過要求一下手指在屏幕上滑動的速度呢!我們可以在滑動事件產生的時候去獲取到滑動的事件,然後建立一個算法去計算手指的滑動速度,這顯得有點複雜,需要數學功底,還有多線程,以及時間的控制。今天我們將爲大家介紹一種簡單的實現方法—-速度跟蹤(VelocityTracker)
關鍵代碼:
由於VelocityTracker的構造方法是private的,因此我們只能使用obain()去獲得一個實例
VelocityTracker mVelocityTracker = VelocityTracker.obtain();
然後爲mVelocityTracker傳入MotionEvent事件
mVelocityTracker.addMovement(event);
接着我們要設置我們獲取的速度是從哪個時間到現在的,如果設置爲1000就是1秒前到現在,手指的平均滑動速度,得到的值是以像素計的,單位pix/s
mVelocityTracker.computeCurrentVelocity(1000);
最後我們就可以獲取我們設置的這段時間內最新的平均速度了
float vx = mVelocityTracker.getXVelocity();
float vy = mVelocityTracker.getYVelocity();
源碼實現:
我們要寫一個Activity去顯示View
VelocityTrackerActivity.java
package com.fht.ada.view;
import com.fht.ada.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class VelocityTrackerActivity extends Activity {
private static final String TAG="VelocityTracker";
private TextView mShowVelocity;
private CustomView mCustomView;
private VelocityTracker mVelocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_velocity_tracker);
mShowVelocity = (TextView) this.findViewById(R.id.show_velocity);
mCustomView = (CustomView) this.findViewById(R.id.custom_view);
mVelocityTracker = VelocityTracker.obtain();
mCustomView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//計算速度
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
float vx = mVelocityTracker.getXVelocity();
float vy = mVelocityTracker.getYVelocity();
Log.i(TAG, vx+" "+vy);
mShowVelocity.setText("vx : "+vx+"\nvy : "+vy);
//存儲數據,讓CustomView進行繪製
mCustomView.data.add(event.getX());
mCustomView.data.add(event.getY());
if (mCustomView.data.size() > 100) {
mCustomView.data.remove(0);
mCustomView.data.remove(0);
}
mCustomView.invalidate();
return true;
}
});
}
}
Activity的佈局
activity_velocity_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/show_velocity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.fht.ada.view.CustomView
android:id="@+id/custom_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
自定義一個View,它用來繪製出我們我們的手指在屏幕上面運動的軌跡
CustomView.java
package com.fht.ada.view;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* 使用VelocityTracker計算手指滑動速度,並且繪製運動軌跡
*
* @author Fht
*
*/
public class CustomView extends View {
private static final String TAG = "VelocityTracker";
public List<Float> data = new ArrayList<Float>();
Paint mPaint = new Paint();
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint.setColor(0xffed1941);
mPaint.setTextSize(10);
}
public CustomView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
Log.i(TAG, "onDraw(Canvas canvas");
float pts[] = new float[data.size()];
for (int i = 0; i < pts.length; i++) {
pts[i] = data.get(i);
}
canvas.drawLines(pts, mPaint);
}
}
至此,我們的代碼就寫完了