Realizing Viewpager function with Gesturedetector

Source: Internet
Author: User
Tags gety

This program : Use the Android gesture event to make a picture browser.
Description : Perhaps the first to see the picture browser, everyone in mind is Viewpager bar. But in fact, with Gesturedetector can also be achieved,
The advantages are:
1. Simple.
2. Consume less memory. After the author tests, this way in 24 pictures still do not appear oom anomaly. And Viewpager, I put 7 in the mobile phone will not be.
(This method actually replaced the original picture, itself will only have a picture in memory)
3. Pictures can be stretched freely
The disadvantages are:
There is no animation effect of sliding.

As for the future choice of which way to achieve, the reader choose their own.

For a detailed usage of gesturedetector see:
http://blog.csdn.net/getchance/article/details/8478993

Steps :
1. in the view file :

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools"android:layout_width="Match_ Parent "android:layout_height=" Match_parent " >                <ImageViewandroid:id="@+id/imageview"android:layout_width= "Fill_parent" android:layout_height="fill_parent" />                        </relativelayout>

2. in the control file :

 PackageCom.example.imagebrowser;Importandroid.app.Activity;ImportAndroid.content.res.Resources;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;ImportAndroid.view.GestureDetector;ImportAndroid.view.MotionEvent;ImportAndroid.widget.ImageView; Public  class mainactivity extends Activity {    //data Field    Private int[] Mimageid;PrivateImageView Mimageview;Private intmcurrentposition=0;Final intMrightflag =0;Final intMleftflag =1;PrivateGesturedetector Gesturedetector;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        InitComponent ();        SetImage ();    Processgesturedetector (); }//the method is used-init UI components    Private void initcomponent() {mimageview= (ImageView) Findviewbyid (R.id.imageview); }//the method is used to set image    Private void setimage() {mimageid=New int[]{r.drawable.pic1,r.drawable.pic2,r.drawable.pic3,r.drawable.pic4, R.drawable.pic5,r.drawable.pic6,r.drawa        BLE.PIC7,R.DRAWABLE.PIC8};           Resources resources = Getbasecontext (). Getresources ();        drawable imagedrawable = resources.getdrawable (Mimageid[mcurrentposition]);    Mimageview.setbackgrounddrawable (imagedrawable); }//the method is used to process Gesturedetector    Private void Processgesturedetector() {Gesturedetector.ongesturelistener Ongesturelistener =NewGesturedetector.simpleongesturelistener () {//Instantiation Listener            @Override               Public Boolean onfling(Motionevent E1, motionevent E2,floatVelocityx,floatVELOCITYY) {floatx = E2.getx ()-E1.getx ();floaty = e2.gety ()-e1.gety ();if(X >0) {Doresult (Mrightflag); }Else if(X <0) {Doresult (Mleftflag); }return true;        }          }; Gesturedetector =NewGesturedetector (mainactivity. This, Ongesturelistener);//Use this listener to generate Gesturedetector instances}//the method Override Ontouchevent Method     Public Boolean ontouchevent(Motionevent event) {returnGesturedetector.ontouchevent (event); }//the method to handle switch Behavior:switch image     Public void Doresult(intAction) {Switch(action) { CaseMrightflag:mcurrentposition= (mcurrentposition+mimageid.length+1)%mimageid.length; SetImage (); Break; CaseMleftflag:mcurrentposition= (mcurrentposition+mimageid.length-1)%mimageid.length; SetImage (); Break; }      }  }

Click here to download the source code

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Realizing Viewpager function with Gesturedetector

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.