android手势识别ViewFlipper触摸动画

今天给大家介绍一下如何实现androd主页面的左右拖动效果。实现起来很简单,就是使用ViewFlipper来将您要来回拖动的View装在一起,然 后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可。比如当手指向左快速滑动时跳转到上一个 View,手指向右快速滑动时跳转到下一个View,本例中使用图片作为各个View的页面,实现左右快速滑动显示不同的图片。

代码片段(3)

[代码] layout

1 <linearlayout android:layout_height="fill_parent"android:layout_width="fill_parent" android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
2     <viewflipper android:id="@+id/flipper"android:layout_below="@+id/CockpitLayout" android:layout_height="fill_parent"android:layout_width="fill_parent">
3         <include android:id="@+id/firstlayout" layout="@layout/first">
4         <include android:id="@+id/secondlayout" layout="@layout/second">
5         <include android:id="@+id/thirdlayout" layout="@layout/third">
6         <include android:id="@+id/fourthlayout" layout="@layout/fourth">
7     </include></include></include></include></viewflipper>
8 </linearlayout>

[代码] ViewFlipper

1 <linearlayout android:gravity="center_vertical"android:layout_height="fill_parent" android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
2   <imageview android:layout_height="wrap_content"android:layout_width="wrap_content" android:src="@drawable/v1">
3 </imageview></linearlayout>

[代码] 我们的Activity需要实现两个接口OnGestureListener,OnTouchListener。

view source
print ?
001 package com.ideasandroid.demo;
002  
003 import android.app.Activity;
004 import android.os.Bundle;
005 import android.view.GestureDetector;
006 import android.view.MotionEvent;
007 import android.view.View;
008 import android.view.GestureDetector.OnGestureListener;
009 import android.view.View.OnTouchListener;
010 import android.view.animation.AccelerateInterpolator;
011 import android.view.animation.Animation;
012 import android.view.animation.TranslateAnimation;
013 import android.widget.ViewFlipper;
014 public class ViewFlipperDemo extends Activity implementsOnGestureListener,OnTouchListener{
015     private ViewFlipper mFlipper;
016     GestureDetector mGestureDetector;
017     private int mCurrentLayoutState;
018     private static final int FLING_MIN_DISTANCE = 100;
019     private static final int FLING_MIN_VELOCITY = 200;
020  
021     @Override
022     public void onCreate(Bundle savedInstanceState) {
023         super.onCreate(savedInstanceState);
024         setContentView(R.layout.main);
025         mFlipper = (ViewFlipper) findViewById(R.id.flipper);
026         //注册一个用于手势识别的类
027         mGestureDetector = new GestureDetector(this);
028         //给mFlipper设置一个listener
029         mFlipper.setOnTouchListener(this);
030         mCurrentLayoutState = 0;
031         //允许长按住ViewFlipper,这样才能识别拖动等手势
032         mFlipper.setLongClickable(true);
033     }
034  
035     /**
036      * 此方法在本例中未用到,可以指定跳转到某个页面
037      * @param switchTo
038      */
039     public void switchLayoutStateTo(int switchTo) {
040         while (mCurrentLayoutState != switchTo) {
041             if (mCurrentLayoutState > switchTo) {
042                 mCurrentLayoutState--;
043                 mFlipper.setInAnimation(inFromLeftAnimation());
044                 mFlipper.setOutAnimation(outToRightAnimation());
045                 mFlipper.showPrevious();
046             else {
047                 mCurrentLayoutState++;
048                 mFlipper.setInAnimation(inFromRightAnimation());
049                 mFlipper.setOutAnimation(outToLeftAnimation());
050                 mFlipper.showNext();
051             }
052  
053         }
054         ;
055     }
056  
057     /**
058      * 定义从右侧进入的动画效果
059      * @return
060      */
061     protected Animation inFromRightAnimation() {
062         Animation inFromRight = new TranslateAnimation(
063                 Animation.RELATIVE_TO_PARENT, +1.0f,
064                 Animation.RELATIVE_TO_PARENT, 0.0f,
065                 Animation.RELATIVE_TO_PARENT, 0.0f,
066                 Animation.RELATIVE_TO_PARENT, 0.0f);
067         inFromRight.setDuration(500);
068         inFromRight.setInterpolator(new AccelerateInterpolator());
069         return inFromRight;
070     }
071  
072     

你可能感兴趣的:(android,velocity,layout,animation,action,distance)