Android uses ViewFlipper to achieve sliding between the left and right sides

Source: Internet
Author: User

This effect is achieved after the results are read. 1. Use the following layout on the main interface of the layout file:
<?xml version="1.0" encoding="utf-8"?><FrameLayout 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/ViewFlipper1"        android:layout_width="fill_parent"         android:layout_height="fill_parent">    </ViewFlipper>                <LinearLayout        android:orientation="horizontal"        android:layout_width="wrap_content"                android:layout_gravity="bottom|center_horizontal"        android:layout_height="wrap_content"                >        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/da"                      android:id="@+id/imageview1"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview2"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview3"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/xiao"            android:id="@+id/imageview4"            />                    </LinearLayout></FrameLayout>

A Brief Introduction to the layout file: the outermost layer is a FrameLayout, and FrameLayout is used to ensure that the following four points are placed on the ViewFlipper. LayoutLayout is centered horizontally under FrameLayout.

2. Use of ViewFlipper

flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);flipper.addView(addImageView(R.drawable.png1o));flipper.addView(addImageView(R.drawable.png2o));flipper.addView(addImageView(R.drawable.png3o));flipper.addView(addView());

Declare a ViewFlipper object in the Activity and find it in the layout. Add three images to ViewFlipper and add a View. The View is obtained from the XML layout file. The layout file is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent"> <Button android: id = "@ + id/button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginBottom = "45dp" android: text = "enter the program" android: textColor = "# 3E3E3E" android: layout_gravity = "center_horizontal"/> </LinearLayout>

There is a Button in the layout file for redirecting to the Activity.

Declare a GestureDetector object in the Activity and allocate the memory in the onCreate method.

Detector = new GestureDetector (this );

If you use this as the parameter, you must make the activity class impllements OnGestureListener interface. Rewrite several methods. Override the onTouchEvent method of the parent class. Write the following in this method:

@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubreturn this.detector.onTouchEvent(event); }

In this way, the detector can accept the message response.

In the OnGestureListener implementation method, determine the user's sliding to switch the interface:

@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {System.out.println("in------------>>>>>>>");if (e1.getX() - e2.getX() > 120) {if (i < 3) {i++;setImage(i);this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.animation_right_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.animation_left_out));this.flipper.showNext();}return true;} else if (e1.getX() - e2.getX() < -120) {if (i > 0) {i--;setImage(i);this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.animation_left_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.animation_right_out));this.flipper.showPrevious();}return true;}return false;}void setImage(int i){for(int j=0;j<4;j++){if(j!=i)iamges[j].setImageResource(R.drawable.xiao);elseiamges[j].setImageResource(R.drawable.da);}}

When the interface is switched, the following four pictures of small ImageView are changed. The switched animation is in the res/anim folder.

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.