Android practice simple tutorial-12th guns (ViewFlipper achieves the magic light effect), androidviewflipper
View Code directly
1. MainActivity. java:
Package org. yayun. demo; import android. app. activity; import android. OS. bundle; import android. view. gestureDetector; import android. view. motionEvent; import android. view. viewGroup. layoutParams; import android. view. animation. animation; import android. view. animation. animationUtils; import android. widget. imageView; import android. widget. viewFlipper; public class MainActivity extends Activity implementsandroid. vi Ew. gestureDetector. onGestureListener {private int [] imgs = {R. drawable. img1, R. drawable. img2, R. drawable. img3}; private GestureDetector gestureDetector; private ViewFlipper viewFlipper; private Activity mActivity; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mActivity = this; viewFlipper = (ViewFlipper) findView ById (R. id. viewflipper); gestureDetector = new GestureDetector (this); // gesture listener for (int I = 0; I 120) {// slide from left to right (left to right) Animation rInAnim = AnimationUtils. loadAnimation (mActivity, R. layout. push_right_in); // slide the gradient (alpha 0.1-> 1.0) on the left to the right. Animation rOutAnim = AnimationUtils. loadAnimation (mActivity, R. layout. push_right_out); // slide the gradient (alpha 1.0-> 0.1) to the right (viewFlipper. setInAnimation (rInAnim); viewFlipper. setOutAnimation (rOutAnim); viewFlipper. showPrevious (); return true;} else if (e2.getX ()-e1.getX () <-120) {// slide from right to left (right to left) animation lInAnim = AnimationUtils. loadAnimation (mActivity, R. layout. push_left_in); // sliding left to the gradient (alpha 0.1-> 1.0) Animation lOutAnim = AnimationUtils. loadAnimation (mActivity, R. layout. push_left_out); // slide the gradient (alpha 1.0-> 0.1) to the right to the left. viewFlipper. setInAnimation (lInAnim); viewFlipper. setOutAnimation (lOutAnim); viewFlipper. showNext (); return true;} public void onLongPress (MotionEvent arg0) {// TODO Auto-generated method stub} public boolean onScroll (MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {// TODO Auto-generated method stubreturn false;} public void onShowPress (MotionEvent arg0) {// TODO Auto-generated method stub} public boolean onSingleTapUp (MotionEvent arg0) {// TODO Auto-generated method stubreturn false ;}}
2. active_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ViewFlipper android:id="@+id/viewflipper" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
Let's take a look at the four animation files below, which is very simple.
1. push_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"/> <! -- Shift --> <alpha android: duration = "1500" android: fromAlpha = "0.1" android: toAlpha = "1.0"/> <! -- Gradient --> </set>
2. push_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>
3. push_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>
4. push_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>
The running instance is as follows:
Thank you.