http://www.javaeye.com/topic/369122
iPhone介面解鎖是用手指划動來實現的, 那麼這個手勢gesture在android裡如何?呢?
Android SDK提供了一個listener類來偵測各種不同的手勢:
SimpleOnGestureListener. 你只需要實現自己所關心的手勢就可以了.
Swipe在android裡面是叫Fling ^_^
首先建立自己的一個手勢detector類: Java代碼
- class MyGestureDetector extends SimpleOnGestureListener {
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
- }
class MyGestureDetector extends SimpleOnGestureListener {@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {}
一個划動的手勢有幾個特徵, 比如是在x或者y軸上近乎直線的划動, 中途路徑的偏差不能太大, 而且划動需要一定的速度, 所以我們定義幾個定量: Java代碼
- private static final int SWIPE_MIN_DISTANCE = 120;
- private static final int SWIPE_MAX_OFF_PATH = 250;
- private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private static final int SWIPE_MIN_DISTANCE = 120;private static final int SWIPE_MAX_OFF_PATH = 250;private static final int SWIPE_THRESHOLD_VELOCITY = 200;
然後在onFling方法中, 判斷是不是一個合理的swipe動作: Java代碼
- if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
- viewFlipper.setInAnimation(slideLeftIn);
- viewFlipper.setOutAnimation(slideLeftOut);
- viewFlipper.showNext();
- } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
- viewFlipper.setInAnimation(slideRightIn);
- viewFlipper.setOutAnimation(slideRightOut);
- viewFlipper.showPrevious();
- }
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {viewFlipper.setInAnimation(slideLeftIn);viewFlipper.setOutAnimation(slideLeftOut);viewFlipper.showNext();} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {viewFlipper.setInAnimation(slideRightIn);viewFlipper.setOutAnimation(slideRightOut);viewFlipper.showPrevious();}
這裡的viewFlipper是含有多個view的一個container, 可以很方便的調用prev/next view, 加上animation動畫, 可以達到一些不錯的效果: Java代碼
- viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
- slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
- slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
- slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
- slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
viewFlipper = (ViewFlipper)findViewById(R.id.flipper); slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in); slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out); slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in); slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
自訂的animation可以查看具體的XML, 比如從左邊進來的一個動畫: Xml代碼
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
- </set>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/></set>
當然最後不要忘記在你的Activity中override onTouch方法來擷取手勢action: Java代碼
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- if (gestureDetector.onTouchEvent(event))
- return true;
- else
- return false;
- }
@Overridepublic boolean onTouchEvent(MotionEvent event) {if (gestureDetector.onTouchEvent(event))return true;elsereturn false;}