Android_imageview simple implementation of picture flipping

Source: Internet
Author: User

1) The ID of the image can be stored in an array , using setimageresource () or setimagedrawable () Method ( put in an array for easy looping )

2 ) already the first image, then click on the "previous page", should Toast tip : is already the first image , no further forward; an image, then click on "Next", you should Toast tip : It is the last image , and no more to turn back.

Give the source code

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal"Tools:context= "com.example.asus.a161304049_gary03." Mainactivity "> <LinearLayout Android:id= "@+id/layoutt"Android:layout_width= "Match_parent"Android:layout_height= "215DP"android:orientation= "Vertical" > <ImageView Android:id= "@+id/imagexianshi"Android:layout_width= "Match_parent"Android:layout_height= "180DP"App:srccompat= "@drawable/qzu1"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal" > <Button Android:id= "@+id/back"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"android:gravity= "Center"Android:text= "Previous"/> <Button Android:id= "@+id/next"Android:layout_width= "100DP"Android:layout_height= "Wrap_content"Android:text= "Next"/> </LinearLayout> </LinearLayout></FrameLayout>
Activity_main.xml

 Packagecom.example.asus.a161304049_gary03;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.ImageView;ImportAndroid.widget.Button;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {//use an array to save the picture       int[] Images =New int[]{r.drawable.qzu1, R.drawable.qzu2, R.DRAWABLE.QZU3, R.drawable.qzu5    }; //Subscript as a picture    intcountimg = 0; //buttons for forward and backward    PrivateButton b1,b2; //use ImageView to store pictures    PrivateImageView Xianshi; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); B1=(Button) Findviewbyid (r.id.back); B2=(Button) Findviewbyid (R.id.next); Xianshi=(ImageView) Findviewbyid (R.id.imagexianshi); Xianshi.setimageresource (images[0]); B1.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//Here's a simple logic .//If the selected picture is the first one, then you can no longer select the previous sheet. Messages that output toast prompts                if(countimg>0) {countimg--; //use Setimageresource to display picturesXianshi.setimageresource (images[countimg]); }Else{toast.maketext (mainactivity). This, "It's the first image, no more forward.", Toast.length_short). Show ();        }            }        }); B2.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//If the selected picture is the last, then you cannot select the next one. Messages that output toast prompts                if(countimg<3) {countimg++;                Xianshi.setimageresource (images[countimg]); }Else{toast.maketext (mainactivity). This, "It's the last image and it's not going to turn back.", Toast.length_short). Show ();    }            }        }); }}
mainactivity

ImageView controls are used to store images, XML attributes are not the focus of this article, and are not described here.

First step: Define member variables

    // use an array to save the picture       int New int []{            r.drawable.qzu1,            r.drawable.qzu2,            r.drawable.qzu3,            r.drawable.qzu5    };     // Subscript as a picture    int countimg = 0;     // Buttons for forward and backward    Private Button b1,b2;     // use ImageView to store pictures    Private ImageView Xianshi;

Step two: Implement the event response mechanism of the button "previous" and "Next"

 protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); B1=(Button) Findviewbyid (r.id.back); B2=(Button) Findviewbyid (R.id.next); Xianshi=(ImageView) Findviewbyid (R.id.imagexianshi); Xianshi.setimageresource (images[0]); B1.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//Here's a simple logic .//If the selected picture is the first one, then you can no longer select the previous sheet. Messages that output toast prompts                if(countimg>0) {countimg--; //use Setimageresource to display picturesXianshi.setimageresource (images[countimg]); }Else{toast.maketext (mainactivity). This, "It's the first image, no more forward.", Toast.length_short). Show ();        }            }        }); B2.setonclicklistener (NewButton.onclicklistener () { Public voidOnClick (View v) {//If the selected picture is the last, then you cannot select the next one. Messages that output toast prompts                if(countimg<3) {countimg++;                Xianshi.setimageresource (images[countimg]); }Else{toast.maketext (mainactivity). This, "It's the last image and it's not going to turn back.", Toast.length_short). Show ();    }            }        }); }

Portal: Message mode Toast.make Several common uses of text

Android_imageview simple implementation of picture flipping

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.