Step by Step _android Development Course [30]_ user interface Viewflipper

Source: Internet
Author: User

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topic: Viewflipper of the user interface
      -
      Viewflipper Multi-View Switching (instance)
      Implementation results:
      Swipe left and right to simulate picture switching on the Welcome screen.

Activity_main.xml:

<?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">                 <viewflipperandroid:id="@+id/viewflipper"android:layout_width ="Fill_parent"android:layout_height="Fill_parent">                                  <!--first page --        <linearlayoutandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:gravity="center">                                                  <ImageViewandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:src="@drawable/a1" />                                                          </linearlayout>        <!--A second page --        <linearlayoutandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:gravity="center">                                                  <ImageViewandroid:layout_width="Fill_parent"android:layout_height= "Fill_parent" android:src="@drawable/a2"android:gravity="center" />                                                                          </linearlayout>        <!--A third page --        <linearlayoutandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:gravity="center">                                                  <ImageViewandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:src="@drawable/a3"android:gravity= "center" />                                                                          </linearlayout>        <!--page Fourth --        <linearlayoutandroid:layout_width="Fill_parent"android:layout_height ="Fill_parent"android:gravity="center">                                                  <ImageViewandroid:layout_width="Fill_parent"android:layout_height= "Fill_parent" android:src="@drawable/a4"android:gravity="center" />                                                                          </linearlayout>    </viewflipper></linearlayout>

Create a Anim file under the Res file, and then create four animation layouts under the Anim file.

Left_to_right_in.xml:

<?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>

Left_to_right_out.xml:

<?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>

Right_to_left_in.xml:

<?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>

Right_to_left_out.xml:

<?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>

Mainactivity.java:

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.MotionEvent;ImportAndroid.widget.ViewFlipper; Public  class mainactivity extends Activity {Viewflipper Viewflipper =NULL;floatStartX; Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);    Init (); }Private void Init() {viewflipper = (viewflipper) This. Findviewbyid (R.id.viewflipper); } Public Boolean ontouchevent(Motionevent event) {Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:startX = Event.getx (); Break; CaseMOTIONEVENT.ACTION_UP:if(Event.getx () > StartX) {//Swipe rightViewflipper.setinanimation ( This, r.anim.left_to_right_in); Viewflipper.setoutanimation ( This, r.anim.left_to_right_out);            Viewflipper.shownext (); }Else if(Event.getx () < StartX) {//Swipe leftViewflipper.setinanimation ( This, r.anim.right_to_left_in); Viewflipper.setoutanimation ( This, r.anim.right_to_left_out);            Viewflipper.showprevious (); } Break; }return Super. Ontouchevent (event); }}

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

Step by Step _android Development Course [30]_ user interface Viewflipper

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.