Android images slide around with gestures

Source: Internet
Author: User

Code:

Image gesture sliding left and right

Package COM. SL. quwei. UI. manage; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. util. displaymetrics; import android. util. log; import android. view. *; import android. view. animation. animationutils; import android. widget. *; import COM. SL. quwei. quweiapplication; import COM. SL. quwei. r; import static android. view. keyevent. keycode_back;/*** created by intellij ide A. * User: Wulong * Date: 12-2-22 * Time: 4: 15 am * to change this template use file | Settings | file templates. */public class agghelp extends activity implements gesturedetector. ongesturelistener {Private Static final string tag = "quwei. agghelp "; private int mscreenwidth = 240; private int mscreenheight = 320; private gesturedetector detector; private viewflipper Flipper; private int mindexel PPIC = 0; private int [] mbglist = {R. drawable. agg_help01, // The number of images required for sliding. drawable. agg_help02, R. drawable. agg_help03, R. drawable. agg_help04, R. drawable. agg_help05, R. drawable. agg_help06};/*** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. agg_bangzhuzhiyin); charsequence Title = Gettitle (); string loginname = (quweiapplication) getapplication ()). getloginusername (); string titlename = aggtools. getnewtitle (title, loginname); settitle (titlename); displaymetrics dm = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (DM); mscreenwidth = DM. widthpixels; mscreenheight = DM. heightpixels; If (mscreenwidth <240) {mscreenwidth = 240;} If (mscreenheight <3 20) {mscreenheight = 320;} Flipper = (viewflipper) This. findviewbyid (R. id. viewflipper01); // Flipper. addview (addbuttonbytext ("button"), new layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); Flipper. addview (addimagebyid (mbglist [mindexhelppic]), new viewgroup. layoutparams (viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. wrap_content); detector = new gesturedetector (this );} Private void backtomaindesk () {// startactivity (new intent (this, aggmanage. class); finish ();} public Boolean onkeydown (INT keycode, keyevent event) {Switch (keycode) {Case keycode_back: backtomaindesk (); Return true; default: break ;} return super. onkeydown (keycode, event);} private view addimagebyid (int id) {imageview IMG = new imageview (this); IMG. setimageresource (ID); IMG. setadjustviewbo UNDS (true); IMG. setmaxwidth (mscreenwidth); IMG. setmaxheight (mscreenheight); IMG. setscaletype (imageview. scaletype. fit_xy); Return IMG;} public view addbuttonbytext (string text) {button BTN = new button (this); BTN. settext (text); Return BTN;} public view addtextbytext (string text) {textview TV = new textview (this); TV. settext (text); TV. setgravity (1); Return TV ;}@ override public Boolean ontouche Vent (motionevent event) {log. I ("Fling", "activity ontouchevent! "); Return this. detector. ontouchevent (event) ;}@ override public Boolean ondown (motionevent e) {// todo auto-generated method stub return false ;} /*** listen to slide */@ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {// todo auto-generated method stub log. I ("Fling", "fling happened! "); If (e1.getx ()-e2.getx ()> 120) {If (++ mindexhelppic> = mbglist. length) {mindexhelppic = mbglist. length-1; backtomaindesk (); Return true;} This. flipper. setinanimation (animationutils. loadanimation (this, R. anim. push_left_in); this. flipper. setoutanimation (animationutils. loadanimation (this, R. anim. push_left_out); // This. flipper. addview (addtextbytext ("text box"), new layoutparams (layoutparams. fill_parent, layoutparams. wrap_content); Flipper. addview (addimagebyid (mbglist [mindexhelppic]), new viewgroup. layoutparams (viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. wrap_content); this. flipper. shownext (); Return true;} else if (e1.getx ()-e2.getx () <-120) {If (-- mindexhelppic <0) {mindexhelppic = 0; return true ;} this. flipper. setinanimation (animationutils. loadanimation (this, R. anim. push_right_in); this. flipper. setoutanimation (animationutils. loadanimation (this, R. anim. push_right_out); this. flipper. showprevious (); Return true;} return true;} @ override public void onlongpress (motionevent e) {// todo auto-generated method stub} @ override public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stub return false;} @ override public void onshowpress (motionevent E) {// todo auto-generated method stub} @ override public Boolean onsingletapup (motionevent e) {// todo auto-generated method stub return false ;}}

Configuration file:

Agg_bangzhuzhiyin.xml

<?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"              android:background="@drawable/agg_sub_bg">    <ViewFlipper android:id="@+id/ViewFlipper01"                 android:layout_width="fill_parent"                 android:layout_height="fill_parent">    </ViewFlipper></LinearLayout>

Put push_left_in.xml in anim

<?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="500" /><alpha android:fromAlpha="0.1" android:toAlpha="1.0"android:duration="500" /></set>

Put push_left_out.xml in anim

<?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="500" /><alpha android:fromAlpha="1.0" android:toAlpha="0.1"android:duration="500" /></set>

Put push_right_in.xml in anim

<?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="500" /><alpha android:fromAlpha="1.0" android:toAlpha="0.1"android:duration="500" /></set>

Put push_right_out.xml in anim

<?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="500" /><alpha android:fromAlpha="1.0" android:toAlpha="0.1"android:duration="500" /></set>

Enables the image to slide around with the gesture

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.