VelocityTracker计算触摸移动速度

 

作用:帮助去跟踪屏幕上的flinging事件,个人认为可能就是手指的触摸移动事件

使用方法:1 调用 VelocityTracker  mVelocityTracker =  VelocityTracker.obtain();   实例化对象 

           2  调用 mVelocityTracker.addMovement(event);   ,为对象添加监听的事件 

           3 调用 mVelocityTracker.computeCurrentVelocity(1, (float)0.01);  计算速度 

           4 mVelocityTracker.getXVelocity( );   和 mVelocityTracker.getYVelocity( );  计算X和Y的速度(可以传递参数来指定移动对象)

            5 使用结束调用 mVelocityTracker.clear();   和 mVelocityTracker.recycle();  来清理对象

 

测试代码: 

 

public class VelocityTrackerTestActivity extends Activity {

	private TextView mInfo;  
	  
    private VelocityTracker mVelocityTracker;  
    private int mMaxVelocity;  
  
    private int mPointerId;  
 
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.velocity_trackerl );
		
		mInfo = (TextView) findViewById( R.id.text);
		
//	    mInfo = new TextView(this);  
//        mInfo.setLines(4);  
//        mInfo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
//        mInfo.setTextColor(Color.WHITE);  
  
//        this.addContentView(mInfo , null);
       // setContentView(mInfo);  
  
        mMaxVelocity = ViewConfiguration.get(this).getMaximumFlingVelocity();  
        
		 
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		 acquireVelocityTracker(event);  
		  switch (action) {  
          case MotionEvent.ACTION_DOWN:  
              //求第一个触点的id, 此时可能有多个触点,但至少一个  
              mPointerId = event.getPointerId(0);  
              break;  

          case MotionEvent.ACTION_MOVE:  
              //求伪瞬时速度  
//        	  mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);  
        	  mVelocityTracker.computeCurrentVelocity(1, (float)0.01); 
              final float velocityX = mVelocityTracker.getXVelocity(mPointerId);  
              final float velocityY = mVelocityTracker.getYVelocity(mPointerId);  
              recodeInfo(velocityX, velocityY);  
              break;  

          case MotionEvent.ACTION_UP:  
              releaseVelocityTracker();  
              break;  

          case MotionEvent.ACTION_CANCEL:  
              releaseVelocityTracker();  
              break;  

          default:  
              break;  
      }  
      return super.onTouchEvent(event);  	
	}
	
	
	
	
    private void acquireVelocityTracker(final MotionEvent event) {  
        if(null == mVelocityTracker) {  
            mVelocityTracker = VelocityTracker.obtain();  
        }  
        mVelocityTracker.addMovement(event);  
    }  
    

    /**  
     * 释放VelocityTracker  
     *  
     * @see android.view.VelocityTracker#clear()  
     * @see android.view.VelocityTracker#recycle()  
     */  
    private void releaseVelocityTracker() {  
        if(null != mVelocityTracker) {  
            mVelocityTracker.clear();  
            mVelocityTracker.recycle();  
            mVelocityTracker = null;  
        }  
    }  
  
    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);  
    }  
    
    
    
}

   

2   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:layout_gravity="center"
    android:gravity="top" 
    android:orientation="vertical" >
     
     <TextView
       android:id="@+id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_margin="1dp"
       android:text="富字体样式"/>
 
            
</LinearLayout>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值