1、布局文件
<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="com.example.helloword.FlipperActivity" >
<ViewFlipper
android:id="@+id/flipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ViewFlipper>
</RelativeLayout>
2、drawable 图片
3、Activity:
package com.example.helloword;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
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 FlipperActivity extends Activity implements View.OnClickListener, GestureDetector.OnGestureListener{
final int distance = 50;
private Animation[] animations = new Animation[4];
private int[] images = new int[]{
R.drawable.img0,R.drawable.img1,R.drawable.img2,R.drawable.img3,
R.drawable.img4
};
private ViewFlipper flipper ;
private GestureDetector detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flipper);
detector = new GestureDetector(FlipperActivity.this,this);
flipper = findViewById(R.id.flipper);
for (int i = 0; i < images.length; i++) {
ImageView img = new ImageView(this);
img.setImageResource(images[i]);
flipper.addView(img);
}
animations[0] = AnimationUtils.loadAnimation(this, R.anim.abc_slide_in_bottom);
animations[1] = AnimationUtils.loadAnimation(this, R.anim.abc_slide_out_bottom);
animations[2] = AnimationUtils.loadAnimation(this, R.anim.abc_slide_in_top);
animations[3] = AnimationUtils.loadAnimation(this, R.anim.abc_slide_out_bottom);
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
if(arg0.getY()-arg1.getY()>distance){
flipper.setInAnimation(animations[2]);
flipper.setOutAnimation(animations[1]);
flipper.showPrevious();
return true;
}else if(arg1.getY()-arg0.getY()>distance){
flipper.setInAnimation(animations[0]);
flipper.setOutAnimation(animations[3]);
flipper.showNext();
return true;
}
return false;
}
@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;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return detector.onTouchEvent(event);
}
}