Use viewflipper and translate animations to achieve screen-sliding switching-translate

Source: Internet
Author: User

 

The android. widget. viewanimator class inherits from framelayout. The viewanimator class is used to provide animation effects for view switching in framelayout. This class has the following animation-related functions:

L setinanimation: Set the animation used when the view enters the screen. This function has two versions, one of which accepts a single parameter and the type is Android. view. animation. animation; one accepts two parameters, whose types are context and INT, respectively, the context object and the resourceid that defines the animation.

  • Setoutanimation: Specifies the animation used when the view exits the screen. The setinanimation parameter function is the same.
  • Shownext: Call this function to display the next view in framelayout.
  • Showprevious: Call this function to display the previous view in framelayout.

Generally, viewanimator is not directly used. Instead, viewflipper and viewswitcher are used. Viewflipper can be used to specify the switching effect between multiple views in framelayout. It can be specified at one time or a separate effect can be specified during each switching. This class provides the following functions:

  • Isflipping: used to determine whether view switching is in progress
  • Setfilpinterval: set the time interval for switching between views.
  • Startflipping: Use the interval set above to start switching all views.
  • Stopflipping: Stop view switching

 

Note that the animation is implemented in the same activity. Different pictures are from viewflipper.

Step 1: Create your own layout file containing the viewfipper control in the layout folder. Each linearlayout wrapped in viewflipper represents a screen, and the first screen is displayed by default,
You can use shownext () in viewflipper to view the next screen, and use the showprevious () method to view the previous screen.
The layout file I defined is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Xmlns: Android = "http://schemas.android.com/apk/res/android">
<Viewflipper
Android: Id = "@ + ID/viewflipper"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<! -- This is the first page on the current page -->
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "This is the first page"/>
</Linearlayout>

<! -- This is the second page -->
<Linearlayout
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "# ff00ff00">
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "this is the second page"/>
</Linearlayout>
</Viewflipper>
</Linearlayout>

 

Step 2: Define the translate animation, and define the animation that slides from left to right and from right to left respectively. Pay attention to the start position and end position of the animation.
The animation file is as follows:
(1) in_lefttoright.xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<! -- Defines the animation that enters the next page when sliding from left to right -->
<Translate
Android: fromxdelta = "-100%"
Android: toxdelta = "0"
Android: Duration = "3000"/>
</Set>

(2) in_righttoleft.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<! -- Defines the animation that enters the next page when sliding from right to left -->
<Translate
Android: fromxdel= "100%"
Android: toxdelta = "0"
Android: Duration = "3000"/>
</Set>
(3) out_lefttoright.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<! -- Defines the animation for moving the current page from left to right -->
<Translate
Android: fromxdelta = "0"
Android: toxdelta = "100%"
Android: Duration = "3000"/>
</Set>

(4) out_righttoleft.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Set xmlns: Android = "http://schemas.android.com/apk/res/android">
<! -- Defines the animation displayed on the current page when sliding from right to left -->
<Translate
Android: fromxdelta = "0"
Android: toxdelta = "-100%"
Android: Duration = "3000"/>
</Set>

 

Step 3: Apply the animation we have defined in the activity and call the setinanimation (context, animation) and setoutanimation (...) methods in viewflipper.
Note that you need to re-write the ontouchevent (motionevent event) method in the activity to achieve screen sliding effect.
Package com. heima. Android. listview;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. motionevent;
Import Android. widget. viewflipper;

Public class myanimation extends activity {
Viewflipper;
Float startx;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. viewflipper );
Viewflipper = (viewflipper) This. findviewbyid (R. Id. viewflipper );
}
/**
* Process user sliding events
*/
@ Override
Public Boolean ontouchevent (motionevent event ){
Switch (event. getaction ()){
Case motionevent. action_down:
Startx = event. getx ();
Break;

Case motionevent. action_up:
If (event. getx ()> startx ){
Viewflipper. setinanimation (this, R. anim. in_lefttoright); // defines the animation when the next page comes in.
Viewflipper. setoutanimation (this, R. anim. out_lefttoright); // defines the animation on the current page.
Viewflipper. shownext (); // display the next page
} Else if (event. getx () <startx ){
Viewflipper. setinanimation (this, R. anim. in_righttoleft );
Viewflipper. setoutanimation (this, R. anim. out_righttoleft );
Viewflipper. showprevious (); // display the previous page
}
Break;
}
Return super. ontouchevent (event );
}
 

}

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.