activity_main.xml
res/anim/in_leftright.xml
res/anim/in_rightleft.xml
res/anim/out_leftright.xml
res/anim/out_rightleft.xml
MainActivity
package com.example.viewflipper;import android.R.integer;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.widget.ViewFlipper;public class MainActivity extends Activity { private ViewFlipper viewFilpper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewFilpper=(ViewFlipper) findViewById(R.id.viewFlipper_1); } //重写触屏事件监听方法 float startx=0.0F; float endx=0.0F; @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action=event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: startx=event.getX(); break; case MotionEvent.ACTION_UP: //向右滑动 if(event.getX()-startx>10){ viewFilpper.setInAnimation(this, R.anim.in_leftright); viewFilpper.setOutAnimation(this, R.anim.out_leftright); viewFilpper.showNext(); }else if(startx-event.getX()>10){ viewFilpper.setInAnimation(this, R.anim.in_rightleft); viewFilpper.setOutAnimation(this, R.anim.out_rightleft); viewFilpper.showPrevious(); } break; default: break; } return super.onTouchEvent(event); }}