Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發之為activity增加左右手勢識別示例

android開發之為activity增加左右手勢識別示例

編輯:關於Android編程

android開發中為activity增加左右手勢識別,如右滑關閉當前頁面。

復制代碼 代碼如下:
/*
 *  for左右手勢
 *  1.復制下面的內容到目標Activity
 *  2.目標Activity的onCreate()調用initGesture()
 *  3.目標Activity需implements OnTouchListener, OnGestureListener
 */
   private GestureDetector mGestureDetector;
   private int verticalMinDistance = 180;
   private int minVelocity         = 0;

   private void initGesture() {
    mGestureDetector = new GestureDetector((OnGestureListener) this);
   }

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

       if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {

           // 切換Activity
           // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
           // startActivity(intent);
           //Toast.makeText(this, "向左手勢", Toast.LENGTH_SHORT).show();
       } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {

           // 切換Activity
           // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
           // startActivity(intent);
        //Toast.makeText(this, "向右手勢", Toast.LENGTH_SHORT).show();
           finish();
           overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
       }

       return false;
   }

@Override
public void onLongPress(MotionEvent arg0) {
 // TODO Auto-generated method stub

}

@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
  float arg3) {
 // TODO Auto-generated method stub
 return false;
}

@Override
public void onShowPress(MotionEvent arg0) {
 // TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent arg0) {
 // TODO Auto-generated method stub
 return false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 return mGestureDetector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent arg0) {
 // TODO Auto-generated method stub
 return false;
}

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) {
 mGestureDetector.onTouchEvent(ev);
 return super.dispatchTouchEvent(ev);
  } 

push_right_in.xml

復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500"/>
</set>

push_right_out.xml

復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500"/>
</set>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved