布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ViewFlipper
android:id="@+id/vf_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
页面代码:
public class ViewAnimatorActivity extends Activity implements GestureDetector.OnGestureListener {
private ViewFlipper mViewFlipper;
private int[] images = {R.drawable.c0, R.drawable.c1, R.drawable.c2, R.drawable.c3, R.drawable.c4, R.drawable.c5};
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
mDetector = new GestureDetector(this, this);
mViewFlipper = (ViewFlipper)findViewById(R.id.vf_view);
for (int i = 0; i < images.length; i++) {// 添加图片源
ImageView iv = new ImageView(this);
iv.setImageResource(images[i]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
mViewFlipper.addView(iv, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
// mViewFlipper.setAutoStart(true);//设置自动播放
// mViewFlipper.setFlipInterval(2000);//设置时间间隔
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mDetector.onTouchEvent(event);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Animation rInAnim = AnimationUtils.loadAnimation(ViewAnimatorActivity.this, R.anim.anim_view);//效果一
Animation rOutAnim = AnimationUtils.loadAnimation(ViewAnimatorActivity.this, R.anim.anim_view_out); //效果二
if (e2.getY() > e1.getY()) {
mViewFlipper.setInAnimation(rInAnim);
mViewFlipper.setOutAnimation(rOutAnim);
mViewFlipper.showNext();
} else if (e2.getY() < e1.getY()) {
mViewFlipper.setInAnimation(rOutAnim);
mViewFlipper.setOutAnimation(rInAnim);
mViewFlipper.showPrevious();
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
}