Android uses viewflipper animation to toggle screen effects _android

Source: Internet
Author: User
Tags set time

of 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.D
Rawable.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); Dynamic import is viewflipper add a child view for (int i = 0; i < resid.length i++) {Flipper.addview (Getimageview (Resid[i));}//OV Erridependingtransition (r.anim.left_in, r.anim.left_out);
Flipper.setflipinterval (3000);
Flipper.startflipping ();
This will achieve a similar slide effect//For Viewflipper to add animation effects//Flipper.setinanimation (this, r.anim.left_in);
Flipper.setoutanimation (this, r.anim.left_out);
Set the Viewflipper time interval//Flipper.setflipinterval (3000);
Start playing//flipper.startflipping (); Private ImageView Getimageview (int resid) {ImageView ImageView = new ImageView (this);//Imageview.setimageresource (res
ID); Wide height depends entirely on picture size imageview.setbackgroundresource (RESID);//So you can adapt to return imageview;  @Override public boolean ontouchevent (Motionevent event) {//TODO auto-generated Method Stub switch (event.getaction ()) {//Finger down case motionevent.action_down://Just start the finger fall coordinates, get the horizontal coordinates of the x {startx = Event.getx (); break;} Finger Move case Motionevent.action_move: {log.i ("Main", "Number of transfers"); Finger left case MOTIONEVENT.ACTION_UP: {if (Event.getx ()-startx > 100) {//Slide right flipper.setinanimation (this, r.anim.le
FT_IN);
Flipper.setoutanimation (this, r.anim.left_out); Flipper.showprevious ()//Show Previous page; } if (Startx-event.getx () > 100) {//Left sliding flipper.setinanimation (this, r.anim.right_in); Flipper.setoutanimation (thi
s, r.anim.right_out);
Flipper.shownext ();//display the latter page;
} break;
} return Super.ontouchevent (event); } <?xml version= "1.0" encoding= "Utf-8"?> <set "xmlns:android=" > <translate android:duration= "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= "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= "Android:fromxdelta=" "100%p" android:toxdelta= "0"/> </set> <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/a"Ndroid "> <translate android:duration=" 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>

Sentiment: Viewflipper seemingly can be made into the new download application, the user first entered the general description. Finally, make a button, into the application.

1. First add the Viewflipper in the Main.xml file, then instantiate in Mainactivity, setcontext layout to get rid of the method of dynamically importing the data source.

Imported through a resid.length for loop. Creates a new ImageView getimageview (int resid) function for a ImageView object

Inside

Imageview.setimageresource (resid); wide height depends entirely on the size of the picture
Imageview.setbackgroundresource (RESID);//This will be adaptive.

Use the second one better. And then returns an object.

4 XML files were established under Res/anim, Left_in,left_out,right_in,right_out;

Example: Left_in to come in from the left

<translate
android:duration= "2000"//Duration 2s
android:fromxdelta= " -100%p"//
android:toxdelta= "0"/ >

There are 2 ways to implement this

(1) overridependingtransition (r.anim.left_in, r.anim.left_out);

Set time interval 
flipper.setflipinterval (3000);
Flipper.startflipping ();
This will achieve a similar slide effect

(2) Flipper.setinanimation (this, r.anim.left_in);

Flipper.setoutanimation (this, r.anim.left_out);
Setting the Viewflipper interval
flipper.setflipinterval (3000);
Start playing
//flipper.startflipping ();

Then load a Boolean ontouchevent (Motionevent event)

Set a global variable private Float startx;//the position of the finger when it falls

Get by Event.getaction ()

Case motionevent.action_down://Finger drop
{startx = Event.getx ();} --> gets the first horizontal coordinate case
motionevent.action_up://fingers leave
{by judging the event.getx ()-startx>100 to the right to slide, then animate, and show the front side
Startx-event.getx () <100 to slide to the left, then animate, and show the latter side
}
The gesture operation is in the up, and if there is a problem with layer folding in down. Through LOG.I ("Main", "number");

You can see that a little bit of the finger will appear a lot.

Case motionevent.action_move://Fingers Slide

The above is a small set of Android to introduce the use of Viewflipper animation screen effect, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.