ViewFlipper
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".activity.MainActivity"> <ViewFlipper android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewFlipper"> </ViewFlipper> </LinearLayout>
left_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromXDelta="100%p" android:toXDelta="0"/> <alpha android:duration="1500" android:fromAlpha="0.1" android:toAlpha="1.0"/> </set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromXDelta="0" android:toXDelta="-100%p"/> <alpha android:duration="1500" android:fromAlpha="1.0" android:toAlpha="0.1"/> </set>
right_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromXDelta="-100%p" android:toXDelta="0"/> <alpha android:duration="1500" android:fromAlpha="0.1" android:toAlpha="1.0"/> </set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromXDelta="0" android:toXDelta="100%p"/> <alpha android:duration="1500" android:fromAlpha="1.0" android:toAlpha="0.1"/> </set>
MainActivity.java
package com.pingyijinren.helloworld.activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.ImageView; import android.widget.ViewFlipper; import com.pingyijinren.helloworld.R; public class MainActivity extends AppCompatActivity { private ViewFlipper viewFlipper; private GestureDetector gestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewFlipper=(ViewFlipper)findViewById(R.id.viewFlipper); for(int i=0;i<10;i++){ ImageView imageView=new ImageView(this); if(i%2==0){ imageView.setImageResource(R.drawable.first_image); } else{ imageView.setImageResource(R.drawable.second_image); } imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); viewFlipper.addView(imageView); } gestureDetector=new GestureDetector(this, new GestureDetector.OnGestureListener() { @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) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e2.getX()-e1.getX()>120){ if(viewFlipper.getDisplayedChild()>0){ viewFlipper.setInAnimation(MainActivity.this,R.anim.right_in); viewFlipper.setOutAnimation(MainActivity.this,R.anim.right_out); viewFlipper.showPrevious(); } } if(e2.getX()-e1.getX()<-120){ if(viewFlipper.getDisplayedChild()==9){ startActivity(new Intent(MainActivity.this,SecondActivity.class)); } else{ viewFlipper.setOutAnimation(MainActivity.this, R.anim.left_out); viewFlipper.setInAnimation(MainActivity.this, R.anim.left_in); viewFlipper.showNext(); } } return false; } }); } @Override public boolean onTouchEvent(MotionEvent event) { return gestureDetector.onTouchEvent(event); } }