Android,android官網

來源:互聯網
上載者:User

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阿 汗.
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.