main.xml
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ViewFlipper>
</RelativeLayout>
left_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="600"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="600"
/>
</set>
left_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="600"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="600"
/>
</set>
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="600"
/>
<alpha
android:fromAlpha="0.1"
android:toAlpha="1.0"
android:duration="600"
/>
</set>
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="600"
/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="600"
/>
</set>
Main.java
package com.example.myflipper;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
public class Main extends Activity {
private static final String TAG = "MainActivity";
private ViewFlipper viewFlipper;
private GestureDetector detector; //手势检测
Animation leftInAnimation;
Animation leftOutAnimation;
Animation rightInAnimation;
Animation rightOutAnimation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
detector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(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 onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i(TAG, "e1="+e1.getX()+" e2="+e2.getX()+" e1-e2="+(e1.getX()-e2.getX()));
if(e1.getX()-e2.getX()>120){
viewFlipper.setInAnimation(leftInAnimation);
viewFlipper.setOutAnimation(leftOutAnimation);
viewFlipper.showNext();//向右滑动
return true;
}else if(e1.getX()-e2.getY()<-120){
viewFlipper.setInAnimation(rightInAnimation);
viewFlipper.setOutAnimation(rightOutAnimation);
viewFlipper.showPrevious();//向左滑动
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
//往viewFlipper添加View
viewFlipper.addView(getImageView(R.drawable.a1));
viewFlipper.addView(getImageView(R.drawable.a2));
viewFlipper.addView(getImageView(R.drawable.a3));
viewFlipper.addView(getImageView(R.drawable.a4));
viewFlipper.addView(getImageView(R.drawable.a5));
viewFlipper.addView(getImageView(R.drawable.a6));
//动画效果
leftInAnimation = AnimationUtils.loadAnimation(this, R.layout.left_in);
leftOutAnimation = AnimationUtils.loadAnimation(this, R.layout.left_out);
rightInAnimation = AnimationUtils.loadAnimation(this, R.layout.right_in);
rightOutAnimation = AnimationUtils.loadAnimation(this, R.layout.right_out);
}
private ImageView getImageView(int id){
ImageView imageView = new ImageView(this);
imageView.setImageResource(id);
return imageView;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event); //touch事件交给手势处理。
}
}