ViewFlipper实现左滑右滑的效果

首先复制5张图片到我们的drawable-mdpi文件夹下

然后在res下创建anim文件夹及其文件


left_in_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="-100%p"
    android:toXDelta="0%p"
    android:duration="1000">
    
</translate>

left_out_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"
    android:duration="1000">
    
</translate>

right_in_anmi.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="100%p"
    android:toXDelta="0%p"
    android:duration="1000">
    
</translate>

right_out_anmi.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="0%p"
    android:toXDelta="100%p"
    android:duration="1000">
    
</translate>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >


    <ViewFlipper android:id="@+id/viewFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        
    </ViewFlipper>
    


</LinearLayout>

MainActivity.java

package com.example.gesturefliptest;


import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
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 MainActivity extends Activity implements OnGestureListener{


GestureDetector detector;
ViewFlipper viewFlipper;
//定义一个动画数组,用于为ViewFlipper指定切换动画效果
Animation[] animations=new Animation[4];

//定义手势动作两点之间的最小距离
final int FLIP_DISTANCE=50;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detector=new GestureDetector(this, this);
//获得ViewFlipper实例
viewFlipper=(ViewFlipper) super.findViewById(R.id.viewFlipper);
//为ViewFlipper添加ImageView
viewFlipper.addView(addImageView(R.drawable.img1));
viewFlipper.addView(addImageView(R.drawable.img2));
viewFlipper.addView(addImageView(R.drawable.img3));
viewFlipper.addView(addImageView(R.drawable.img4));
viewFlipper.addView(addImageView(R.drawable.img5));

//初始化动画数组
animations[0]=AnimationUtils.loadAnimation(this, R.anim.left_in_anim);
animations[1]=AnimationUtils.loadAnimation(this, R.anim.left_out_anim);
animations[2]=AnimationUtils.loadAnimation(this, R.anim.right_in_anim);
animations[3]=AnimationUtils.loadAnimation(this, R.anim.right_out_anim);


}


//定义一个添加ImageView的方法
public View addImageView(int resId){
ImageView imageView=new ImageView(MainActivity.this);
imageView.setImageResource(resId);
//当图片的长宽超过View的长宽,截取中间部分县市
imageView.setScaleType(ImageView.ScaleType.CENTER);
return imageView;
}

//在该方法里处理翻页操作
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e2.getX()-e1.getX()>FLIP_DISTANCE){
//表明是向右滑动
viewFlipper.setInAnimation(animations[0]);
viewFlipper.setOutAnimation(animations[3]);
//显示上一个内容
viewFlipper.showPrevious();
return true;
}
else if(e1.getX()-e2.getX()>FLIP_DISTANCE){
//表明是向左滑动
viewFlipper.setInAnimation(animations[1]);
viewFlipper.setOutAnimation(animations[2]);
//显示上一个内容
viewFlipper.showNext();
return true;
}

return false;
}


//将Activitiy上的触摸事件交给Detector
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
}


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


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

}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}


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

}


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


}

你可能感兴趣的:(viewflipper,gesturedetector)