Android Gesture Recognition viewflipper touch Animation

Source: Internet
Author: User

Recently, the viewflipper class was used in the project. It seems that the effect is really amazing. Today I tried to do it myself. It is really good.

First, define viewflipper. XML in layout.

<?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/viewFlipper1"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >        <ImageView            android:id="@+id/imageView1"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:src="@drawable/b" />         <ImageView            android:id="@+id/imageView2"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:src="@drawable/c" />          <ImageView            android:id="@+id/imageView3"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:src="@drawable/d" />          <ImageView            android:id="@+id/imageView4"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:src="@drawable/f" />         <ImageView            android:id="@+id/imageView5"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:src="@drawable/g" />    </ViewFlipper></LinearLayout>

Viewflipper contains five images for gesture switching.

Public class viewflipperactivity extends activity implements ontouchlistener, android. view. gesturedetector. ongesturelistener {private viewflipper Flipper; listener; private int mcurrentlayoutstate; Private Static final int completion = 80; Private Static final int completion = 150; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-gen Erated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. viewflipper); Flipper = (viewflipper) This. findviewbyid (R. id. viewflipper1); // register a class mgesturedetector = new gesturedetector (this) for gesture recognition; // set a listener Flipper for mflipper. setontouchlistener (this); mcurrentlayoutstate = 0; // you can hold down viewflipper for a long time to identify Flipper. setlongclickable (true);}/*** this method is not used in this example. You can specify to jump to a page * @ Param swit Chto */Public void switchlayoutstateto (INT switchto) {While (mcurrentlayoutstate! = Switchto) {If (mcurrentlayoutstate> switchto) {mcurrentlayoutstate --; Flipper. setinanimation (infromleftanimation (); Flipper. setoutanimation (outtorightanimation (); Flipper. showprevious ();} else {mcurrentlayoutstate ++; Flipper. setinanimation (infromrightanimation (); Flipper. setoutanimation (outtoleftanimation (); Flipper. shownext () ;}}/ *** defines the animation effect to enter from the right * @ return */protected animation infromrightanimation () {animation infromright = new translateanimation (animation. relative_to_parent, + 1.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f); infromright. setduration (200); infromright. setinterpolator (New accelerateinterpolator (); Return infromright;}/*** defines the animation effect to exit from the left * @ return */protected animation outtoleftanimation () {animation outtoleft = new translateanimation (animation. relative_to_parent, 0.0f, animation. relative_to_parent,-1.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f); outtoleft. setduration( 200); outtoleft. setinterpolator (New accelerateinterpolator (); Return outtoleft;}/*** defines the animation effect to enter from the left * @ return */protected animation infromleftanimation () {animation infromleft = new translateanimation (animation. relative_to_parent,-1.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f); infromleft. setduration (200); infromleft. setinterpolator (New accelerateinterpolator (); Return infromleft;}/*** defines the animation effect when exiting from the right side * @ return */protected animation outtorightanimation () {animation outtoright = new translateanimation (animation. relative_to_parent, 0.0f, animation. relative_to_parent, + 1.0f, animation. relative_to_parent, 0.0f, animation. relative_to_parent, 0.0f); outtoright. setduration (1, 200); outtoright. setinterpolator (New accelerateinterpolator (); Return outtoright;} public Boolean ondown (motionevent e) {// todo auto-generated method stubreturn false ;} /** the event is triggered when you press the touch screen and move quickly. * E1: 1st action_down motionevent * E2: The last action_move motionevent * velocityx: The moving speed on the X axis, pixel/Second * velocityy: The moving speed on the Y axis. pixel/Second * trigger condition: * The coordinate displacement of the X axis is greater than that of fling_min_distance, and the moving speed is greater than fling_min_velocity pixels/Second */Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {If (e1.getx ()-e2.getx ()> fling_min_distance & math. ABS (velocityx)> fling_min_velocity) {// sets the animation Flipper used when the view enters the screen while sliding on the left. setinanimation (infromrightanimation (); // sets the animation Flipper used when the view exits the screen. setoutanimation (outtoleftanimation (); Flipper. shownext ();} else if (e2.getx ()-e1.getx ()> fling_min_distance & math. ABS (velocityx)> fling_min_velocity) {// Flipper when the image slides on the right. setinanimation (infromleftanimation (); Flipper. setoutanimation (outtorightanimation (); Flipper. showprevious ();} return false;} public void onlongpress (motionevent e) {// todo auto-generated method stub} public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stubreturn false;} public void onshowpress (motionevent e) {// todo auto-generated method stub} public Boolean onsingletapup (motionevent E) {// todo auto-generated method stubreturn false;} public Boolean ontouch (view V, motionevent event) {// todo auto-generated method stub // the touch screen event must be handed over to the gesture recognition class for processing (it will be troublesome to handle it by yourself) return mgesturedetector. ontouchevent (event );}}

In this way, you can check the effect.

Because the gesture recognition effect is not very good, you can see the effect after you implement it yourself!

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.