下面是一个利用手势功能做的一个翻页效果
上图实际是一个ViewFlipper容器,里面放了5张图片,从右向左划动,看下一张图片,从左向右划动,看上一张图片
同时在划动时播放动画(缩放和透明度的改变),这样看起来更生动一些
动画文件
left_in.xml,图片从右向左滑动时的动画,View进入屏幕时候使用的动画
向右划动的动画类似,不写出来了
package WangLi.IO.GestureFlip;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class GestureFlip extends Activity
implements OnGestureListener
{
//ViewFlipper实例
ViewFlipper flipper;
//定义手势检测器实例
GestureDetector detector;
//定义一个动画数组,用于为ViewFlipper指定切换动画效果
Animation[] animations = new Animation[4];
//定义手势动作两点之间的最小距离
final int FLIP_DISTANCE = 50;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建手势检测器
detector = new GestureDetector(this);
//获得ViewFlipper实例
flipper = (ViewFlipper)this.findViewById(R.id.flipper);
//为ViewFlipper添加5个ImageView组件
flipper.addView(addImageView(R.drawable.one));
flipper.addView(addImageView(R.drawable.two));
flipper.addView(addImageView(R.drawable.three));
flipper.addView(addImageView(R.drawable.four));
flipper.addView(addImageView(R.drawable.five));
//初始化Animation数组
animations[0] = AnimationUtils.loadAnimation(this, R.anim.left_in);
animations[1] = AnimationUtils.loadAnimation(this, R.anim.left_out);
animations[2] = AnimationUtils.loadAnimation(this, R.anim.right_in);
animations[3] = AnimationUtils.loadAnimation(this, R.anim.right_out);
}
private View addImageView(int resId) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(this);
imageView.setImageResource(resId);
imageView.setScaleType(ImageView.ScaleType.CENTER);
return imageView;
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
//如果第一触点事件的x坐标大于第二个触点事件的x坐标超过FLIP_DISTANCE
//也就是手势从右向左滑动
if(event1.getX() - event2.getX() > FLIP_DISTANCE)
{
//为flipper设置切换的动画效果
flipper.setInAnimation(animations[0]);
flipper.setOutAnimation(animations[1]);
flipper.showPrevious();
return true;
}
if(event2.getX() - event1.getX() > FLIP_DISTANCE)
{
//为flipper设置切换的动画效果
flipper.setInAnimation(animations[2]);
flipper.setOutAnimation(animations[3]);
flipper.showNext();
return true;
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
//将该Activity上的触碰事件交给GestureDetector处理
return detector.onTouchEvent(event);
}
@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;
}
}