Viewflipper animation switching screen, viewflipper animation Switching

Source: Internet
Author: User

Viewflipper animation switching screen, viewflipper animation Switching

 

The entire project

Package com. example. viewflipper; import android. r. integer; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. gestureDetector. onDoubleTapListener; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. view. window; import android. widget. imageView; import android. widget. viewFlipper; public class MainActivity extends Activity {private ViewFlipper flipper; private float startX; private int resId [] = {R. drawable. ic_girl2, R. drawable. ic_girl3, R. drawable. ic_girl4, R. drawable. ic_girl5, };@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. main); flipper = (ViewFlipper) findViewById (R. id. viewflipper); // Add the ViewFlipper sub-VIEW for (int I = 0; I <resId. length; I ++) {flipper. addView (getImageView (resId [I]);} // overridePendingTransition (R. anim. left_in, R. anim. left_out); // flipper. setFlipInterval (3000); flipper. startFlipping (); // This will achieve a slide-like effect. // Add an animation effect to viewFlipper. // flipper. setInAnimation (this, R. anim. left_in); // flipper. setOutAnimation (this, R. anim. left_out); // set the time interval of ViewFlipper // flipper. setFlipInterval (3000); // start playing // flipper. startFlipping ();} private ImageView getImageView (int resId) {ImageView imageView = new ImageView (this); // imageView. setImageResource (resId); the width and height depend entirely on the image size imageView. setBackgroundResource (resId); // You can automatically return imageView;} @ Override public boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stub switch (event. getAction () {// drop case MotionEvent by finger. ACTION_DOWN: // the coordinate of the finger falling at the beginning to obtain the x {startX = event of the horizontal coordinate. getX (); break;} // move the case MotionEvent by finger. ACTION_MOVE: {Log. I ("Main", "transfer times"); break;} // exit case MotionEvent with your fingers. ACTION_UP: {if (event. getX ()-startX> 100) {// slide flipper to the right. setInAnimation (this, R. anim. left_in); flipper. setOutAnimation (this, R. anim. left_out); flipper. showPrevious (); // display the previous page;} if (startX-event. getX ()> 100) {// slide flipper to the left. setInAnimation (this, R. anim. right_in); flipper. setOutAnimation (this, R. anim. right_out); flipper. showNext (); // display the last page;} break;} return super. onTouchEvent (event );}}
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="2000"        android:fromXDelta="-100%p"        android:toXDelta="0" />    </set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="2000"        android:fromXDelta="0"        android:toXDelta="100%p" />  </set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="2000"        android:fromXDelta="100%p"        android:toXDelta="0" />  </set>
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="2000"        android:fromXDelta="0"        android:toXDelta="-100%p" /></set>
<?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:orientation="vertical" >    <ViewFlipper        android:id="@+id/viewflipper"        android:layout_width="match_parent"        android:layout_height="match_parent">    </ViewFlipper></LinearLayout>

 

Perception: ViewFlipper seems to be able to be used as a new download application, which is a rough description of the user's first entry .. Finally, click a button to enter the application.

1. Add viewflipper to the main. xml file and instantiate it in MainActivity. To change the setContext layout, use the dynamic data source import method.

It is imported through a resId. length for loop. Create an ImageView object's ImageView getImageView (int resId) Function

Inside

// ImageView. setImageResource (resId); the width and height depend entirely on the image size.
ImageView. setBackgroundResource (resId); // adaptive

Use the second one. Then, an object is returned.

Create four xml files, left_in, left_out, right_in, and right_out under res/anim respectively;

 

For example, left_in comes in from the left.

<Translate
Android: duration = "2000" // The duration is 2 s
Android: fromXDelta = "-100% p "//
Android: toXDelta = "0"/>

There are two implementation methods

(1) overridePendingTransition (R. anim. left_in, R. anim. left_out );

// Set the interval

Flipper. setFlipInterval (3000 );

Flipper. startFlipping ();
// This will achieve a slide-like effect

 


(2) flipper. setInAnimation (this, R. anim. left_in );
Flipper. setOutAnimation (this, R. anim. left_out );
// Set the time interval of ViewFlipper
Flipper. setFlipInterval (3000 );
// Start playing
// Flipper. startFlipping ();

 

Then load a boolean onTouchEvent (MotionEvent event)

Set a global variable private Float startX; // The position when the finger falls

Obtained Through event. getAction ()

Case MotionEvent. ACTION_DOWN: // when the finger falls

{StartX = event. getX () ;}--> get the starting abscissa

 

 

Case MotionEvent. ACTION_UP: // exit with your finger

{Slide to the right by judging event. getX ()-startX> 100, then implement the animation and display the first side

StartX-event.getX () <100 judge to slide left, then implement the animation and display the back side
}

// The gesture operation must be in up. If it is down, the layer is folded. Log. I ("Main", "times ");

We can see that there will be a lot of finger movement.

Case MotionEvent. ACTION_MOVE: // when the finger slides

...............

OK.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.