Android,android官網
VelocityTracker
主要應用於touch event, VelocityTracker通過跟蹤一連串事件Realtime Compute出當前的速度。
方法
//擷取一個VelocityTracker對象, 用完後記得回收 //回收後代表你不需要使用了,系統將此對象在此分配到其他要求者 static public VelocityTracker obtain(); public void recycle(); //計算當前速度, 其中units是單位表示, 1代表px/毫秒, 1000代表px/秒, .. //maxVelocity此次計算速度你想要的最大值 public void computeCurrentVelocity(int units, float maxVelocity); //經過一次computeCurrentVelocity後你就可以用一下幾個方法擷取此次計算的值 //id是touch event觸摸點的ID, 來為多點觸控標識,有這個標識在計算時可以忽略 //其他觸點幹擾,當然幹擾肯定是有的 public float getXVelocity(); public float getYVelocity(); public float getXVelocity(int id); public float getYVelocity(int id);
Code
public class VelocityTrackerTest extends Activity { private TextView mInfo; private VelocityTracker mVelocityTracker; private int mMaxVelocity; private int mPointerId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mInfo = new TextView(this); mInfo.setLines(4); mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); mInfo.setTextColor(Color.WHITE); setContentView(mInfo); mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity(); } @Override public boolean onTouchEvent(MotionEvent event) { if(null == mVelocityTracker) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); final VelocityTracker verTracker = mVelocityTracker; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: //求第一個觸點的id, 此時可能有多個觸點,但至少一個 mPointerId = event.getPointerId(0); break; case MotionEvent.ACTION_MOVE: //求偽瞬時速度 verTracker.computeCurrentVelocity(1000, mMaxVelocity); float velocityX = verTracker.getXVelocity(mPointerId); float velocityY = verTracker.getYVelocity(mPointerId); recodeInfo(velocityX, velocityY); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if(null != mVelocityTracker) { mVelocityTracker.clear(); mVelocityTracker.recycle(); mVelocityTracker = null; } break; default: break; } return super.onTouchEvent(event); } private static final String sFormatStr = "velocityX=%f\nvelocityY=%f"; /** * 記錄當前速度 * * @param velocityX x軸速度 * @param velocityY y軸速度 */ private void recodeInfo(final float velocityX, final float velocityY) { final String info = String.format(sFormatStr, velocityX, velocityY); mInfo.setText(info); } }我是天王蓋地虎的分割線
參考:http://blog.csdn.net/bingxianwu/article/details/7446799
Android軟體開發用什語言?
Javascript精通還做什麼android阿 汗.
Android軟體開發用什語言?
Javascript精通還做什麼android阿 汗.