Android learning notes ---- simple introduction to viewflipper

Source: Internet
Author: User

Viewflipper is a simple implementation provided by Android, multiple view switching controls, and animation effects can be added.

First, let's take a look at how viewflipper is described in the document.

Simple viewanimator that 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.

Viewflipper enables switching between two or more views, provided that these views have been added to viewflipper, and only one view can be displayed at a time. You can also add animation effects to make the effects more dazzling.

Then, let's take a look at how viewflipper is used.

In fact, its usage is very simple. My understanding is: it is actually a layout that can switch sub-views. In this case, everyone should know how to use it.

Okay, let's talk about it.Code:

First, create two animation files, create the anim folder in res, and then create the files flipper_in.xml and flipper_out.xml. Defines the effect of the disappearance and appearance of the sub-space switching.

Flipper_in.xml:

 
<? XML version = "1.0" encoding = "UTF-8"?> <Set xmlns: Android = "http://schemas.android.com/apk/res/android"> <translate Android: fromxdelta = "-100% P" Android: toxdelta = "0" Android: duration = "1000"/> <Alpha Android: fromalpha = "0.0" Android: toalpha = "1.0" Android: Duration = "1000"/> </set>

Flipper_out.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Set xmlns: Android = "http://schemas.android.com/apk/res/android"> <translate Android: fromxdelta = "0" Android: toxdelta = "100% P" Android: Duration = "1000"/> <Alpha Android: fromalpha = "1.0" Android: toalpha = "0.0" Android: Duration = "1000"/> </set>

Next is the layout file main. xml of the main view.

<? 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"> <button Android: id = "@ + ID/previusone" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "show previous"/> <viewflipper Android: id = "@ + ID/mviewflipper" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_gravity = "center"> </viewflipper> <button Android: id = "@ + ID/nextone" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "show next"/> </linearlayout>

Here, we can only describe that the subview is directly deployed in viewflipper, just like layout. However, I use the following code to add a subview to the Code:

Package CSY. viewflipperdemo; import android. app. activity; import android. content. context; import android. content. dialoginterface; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. textview; import android. widget. viewflipper; public class viewflipperdemoactivity extends activity implements onclicklistener {private context; private button nextbtn; private button prebtn; private viewflipper mviewflipper;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Context = viewflipperdemoactivity. this; initviews ();} public void initviews () {nextbtn = (button) findviewbyid (R. id. nextone); nextbtn. setonclicklistener (this); prebtn = (button) findviewbyid (R. id. previusone); prebtn. setonclicklistener (this);/** add a sub-space for mviewflipper here. Of course, you can also add */mviewflipper = (viewflipper) findviewbyid (R. id. mviewflipper); mviewflipper. addview (createtextviewbytext ("This is the first textview"); mviewflipper. addview (createtextviewbytext ("this is the second textview"); mviewflipper. addview (createtextviewbytext ("this is the third textview"); mviewflipper. addview (createtextviewbytext ("this is the fourth textview");/** set the expected animation effect for mviewflipper */mviewflipper. setinanimation (context, R. anim. flipper_in); mviewflipper. setoutanimation (context, R. anim. flipper_out);} public view createtextviewbytext (string text) {textview view = new textview (context); view. settext (text); Return view ;}@ overridepublic void onclick (view v) {// todo auto-generated method stubswitch (v. GETID () {case R. id. nextone: mviewflipper. shownext (); break; case R. id. previusone: mviewflipper. showprevious (); break; default: Break ;}}}

The code is relatively simple, so there should be no major problems. In this way, we can simply understand the usage of the viewflipper control.

Below is the source code http://download.csdn.net/detail/chenshaoyang0011/4082405 of the example

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.