Android uses Viewflipper to achieve left and right swipe effects

Source: Internet
Author: User



Public
classMainactivityextendsActivityImplementsOngesturelistener {Private Static FinalString TAG = "Mainactivity"; PrivateViewflipper Viewflipper; PrivateGesturedetector detector;//gesture DetectionAnimation leftinanimation; Animation leftoutanimation; Animation rightinanimation; Animation rightoutanimation; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); Viewflipper=(Viewflipper) Findviewbyid (r.id.viewflipper); Detector=NewGesturedetector ( This); //add view to ViewflipperViewflipper.addview (Getimageview (r.drawable.new_feature_1)); Viewflipper.addview (Getimageview (r.drawable.new_feature_2)); Viewflipper.addview (Getimageview (r.drawable.new_feature_3)); Viewflipper.addview (Getimageview (r.drawable.new_feature_4)); Viewflipper.addview (Getimageview (r.drawable.new_feature_5)); Viewflipper.addview (Getimageview (r.drawable.new_feature_6)); //animation effectsLeftinanimation = Animationutils.loadanimation ( This, r.anim.left_in); Leftoutanimation= Animationutils.loadanimation ( This, r.anim.left_out); Rightinanimation= Animationutils.loadanimation ( This, r.anim.right_in); Rightoutanimation= Animationutils.loadanimation ( This, r.anim.right_out); } PrivateImageView Getimageview (intID) {ImageView ImageView=NewImageView ( This); Imageview.setimageresource (ID); returnImageView; } @Override Public Booleanontouchevent (Motionevent event) {return This. Detector.ontouchevent (event);//The touch event is handed to gesture processing. } @Override Public BooleanOndown (motionevent e) {//TODO auto-generated Method Stub return true; } @Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {log.i (TAG,"E1=" +e1.getx () + "e2=" +e2.getx () + "e1-e2=" + (E1.getx ()-E2.getx ())); if(E1.getx ()-e2.getx () >120) {viewflipper.setinanimation (leftinanimation); Viewflipper.setoutanimation (leftoutanimation); Viewflipper.shownext ();//Swipe Right return true; }Else if(E1.getx ()-e2.gety () <-120) {viewflipper.setinanimation (rightinanimation); Viewflipper.setoutanimation (rightoutanimation); Viewflipper.showprevious ();//Swipe left return true; } return false; } @Override Public voidonlongpress (motionevent e) {//TODO auto-generated Method Stub} @Override Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) { //TODO auto-generated Method Stub return false; } @Override Public voidonshowpress (motionevent e) {//TODO auto-generated Method Stub} @Override Public BooleanOnsingletapup (motionevent e) {//TODO auto-generated Method Stub return false; } }

The layout file is as follows:

<Relativelayoutxmlns: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" >   <ViewflipperAndroid:id= "@+id/viewflipper"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"       ></Viewflipper></Relativelayout>

As follows:

The overall effect and viewpager consistent, the benefit is not to consider the viewpager preload situation, the downside is that there is no viewpager in the process of page transitions animation effect

The animation effects are laid out as follows:

Left_in.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= "100%p"Android:toxdelta= "0"android:duration= "All"        />    <AlphaAndroid:fromalpha= "0.1"Android:toalpha= "1.0"android:duration= "All"        /></Set>

Left_out.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= "0"Android:toxdelta= " -100%p"android:duration= "All"        />    <AlphaAndroid:fromalpha= "1.0"Android:toalpha= "0.1"android:duration= "All"        /></Set>

Right_in.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= " -100%p"Android:toxdelta= "0"android:duration= "All"        />    <AlphaAndroid:fromalpha= "0.1"Android:toalpha= "1.0"android:duration= "All"        /></Set>

Right_out.xml

<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android">    <TranslateAndroid:fromxdelta= "0"Android:toxdelta= "100%p"android:duration= "All"        />    <AlphaAndroid:fromalpha= "1.0"Android:toalpha= "0.1"android:duration= "All"        /></Set>

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.