public class MainActivity extends Activity { private ViewFlipper viewFlipper; private float startX; private Animation in_lefttoright; private Animation out_lefttoright; private Animation in_righttoleft; private Animation out_righttoleft; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright); out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright); in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft); out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft); //ViewFlipper 中的每一个组件都被视为一个页面 viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper); } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_DOWN){ startX = event.getX(); }else if(event.getAction()==MotionEvent.ACTION_UP){ float endX = event.getX(); if(endX > startX){ //设置进入的动画 viewFlipper.setInAnimation(in_lefttoright); //设置出去的动画 viewFlipper.setOutAnimation(out_lefttoright); viewFlipper.showNext();//显示下一页 }else if(endX < startX){ viewFlipper.setInAnimation(in_righttoleft); viewFlipper.setOutAnimation(out_righttoleft); viewFlipper.showPrevious();//显示前一页 } return true; } return super.onTouchEvent(event); } public void openActivity(View v){ Intent intent = new Intent(this, OtherActivity.class); startActivity(intent); //为Activity设置切换动画 this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);//实现Activity切换动画效果 } }
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="-100%p" <!-- 100%p 标示屏幕的宽度--> android:toXDelta="0" android:duration="5000" /> </set>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ViewFlipper <!--ViewFlipper 中的每一个组件都被视为一个显示页面--> android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/viewFlipper" > <!-- 第一页 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第一页" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打开新Activity" android:onClick="openActivity" /> </LinearLayout> <!-- 第二页 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#339900" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="第二页" /> </LinearLayout> </ViewFlipper> </LinearLayout>
来源:https://www.cnblogs.com/heml/p/3518023.html