Android uses viewflipper to implement screen switching animation Effects

Source: Internet
Author: User
1. Screen switching refers to the switching on the screen of the same activity. The longest case is that there are multiple pages in a framelayout, such as a system setting page; a personalized settings page. 2. Introduction to viewfilpper viewflipperextends
Viewanimator
Java. Lang. Object
Bytes Android. View. View

Bytes Android. View. viewgroup


Bytes Android. widget. framelayout



Bytes Android. widget. viewanimator




Bytes Android. widget. viewflipper
Class Overview

SimpleViewAnimatorThat will animate between two or more views that have been added to it. Only one child
Is shown at a time. If requested, can automatically flip between each child at a regular interval.

It means that two or more views are added with animations between simple viewanimator. Only one child is displayed at a time. If needed, each child can automatically flip at a fixed interval.

This class inherits the framelayout class. The viewanimator class provides animation effects for view switching in framelayout.

This class has the following animation-related functions:

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.

3. First, let's take a look at the XML file defining the four animations: in_leftright.xml -- Entering the screen from left to right

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="3000"        android:fromXDelta="-100%p"        android:toXDelta="0" /></set>

Out_leftright.xml -- display from left to right

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="3000"        android:fromXDelta="0"        android:toXDelta="100%p" /></set>

In_rightleft.xml -- enter the screen from right to left

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="3000"        android:fromXDelta="100%p"        android:toXDelta="0" /></set>

Out_rightleft.xml -- display from right to left

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="3000"        android:fromXDelta="100%p"        android:toXDelta="0" /></set>

4. define the main. xml file

<? 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"> <! -- The first page --> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center"> <imageview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/A1"/> </linearlayout> <! -- The second page --> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center"> <imageview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/A2" Android: gravity = "center"/> </linearlayout> <! -- Third page --> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center"> <imageview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/A3" Android: gravity = "center"/> </linearlayout> <! -- Fourth page --> <linearlayout Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center"> <imageview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/A4" Android: gravity = "center"/> </linearlayout> </viewflipper> </linearlayout>

5. Java code implementation:

Public class switchtest2activity extends activity {viewflipper = NULL; float startx; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {viewflipper = (viewflipper) This. findviewbyid (R. id. viewflipper);} public Boolean ontouchevent (motionevent event) {Switch (event. getaction () {Case motionevent. action_down: startx = event. getx (); break; Case motionevent. action_up: If (event. getx ()> startx) {// slide viewflipper to the right. setinanimation (this, R. anim. in_leftright); viewflipper. setoutanimation (this, R. anim. out_leftright); viewflipper. shownext ();} else if (event. getx () <startx) {// slide to the left viewflipper. setinanimation (this, R. anim. in_rightleft); viewflipper. setoutanimation (this, R. anim. out_rightleft); viewflipper. showprevious ();} break;} return Super. ontouchevent (event );}}

6. I can't see it here. I can paste some pictures !! Result of sliding from left to right



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.