Android Small Example: Use the reflection mechanism to read pictures to make a picture browser

Source: Internet
Author: User


                               

Project folder:

  This example can be used for beginners ' reference exercises, if there is something wrong with it >-<

The man under the curtain

  Java code (MAINACTIVITY.JAVA):
  

 Packagecom.example.imageswitchtest;ImportJava.lang.reflect.Field;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Private int[] IMGs =NULL;//Store Picture Address    Private intCurrentimgindex;//identification number of the current picture    PrivateImageView img =NULL ; PrivateButton Btnup =NULL ; PrivateButton Btnnext =NULL ; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (R.layout.activity_main); IMG=(ImageView) Findviewbyid (r.id.img); Btnup=(Button) Findviewbyid (R.ID.BTN_UP); Btnnext=(Button) Findviewbyid (R.id.btn_next); //Initializeinit (); Img.setimageresource (imgs[1]); Currentimgindex= 1; //set Listener events for buttonBtnup.setonclicklistener (NewButtononclick ()); Btnnext.setonclicklistener (NewButtononclick ()); }       /*** Use the reflection mechanism to read the picture resources, stored in the IMGs array*/     Public voidinit () {field[] fields= R.drawable.class. GetFields ();//Reflection Read R.drawableIMGs =New int[Fields.length];//instantiating an IMGs array         for(inti = 0; i < fields.length; i++) {//traversing Fields            Try {                //the problem with the test is that the Imgs[0] value defaults to Ic_launcherLOG.D ("field[" + i + "]", fields[i].getname () + "");//Test                if(Fields[i].getname (). IndexOf ("Pic_") >-1) {//Filter the image to start with Pic_Imgs[i] = Fields[i].getint (r.drawable.class);//Gets the image storage address, stored in the IMGs arrayLOG.D ("field[" + i + "]", fields[i].getname () + "");//Test                }            } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); }        }    }        /**     *      * @authorBlack Sky * @ Click event Listener Processing class with Button*/    Private classButtononclickImplementsview.onclicklistener{@Override Public voidOnClick (view view) {intid = View.getid ();//Gets the component ID that the monitor hears the clicked on            Switch(ID) {//Identification ID             CaseR.ID.BTN_UP://Previous Picture button clicked on the execution method                if(currentimgindex>1) {//The current picture identification number is not the first onecurrentimgindex--;//identification number-1                                    }Else{//It's the first one .//the tip is the first pictureToast.maketext (mainactivity. This, "This is already the first picture!" ", Toast.length_short). Show (); }                 Break;  CaseR.id.btn_next://Next Picture button to click on the execution method                if(currentimgindex//The current picture identification number is not the last onecurrentimgindex++;//Identification number +1}Else{//It's the last one .//tip is the last pictureToast.maketext (mainactivity. This, "This is the last picture!" ", Toast.length_short). Show (); }                 Break; } img.setimageresource (Imgs[currentimgindex]);//Show Pictures        }            }    }

  XML code (ACTIVITY_MAIN.XML):

<LinearLayoutxmlns: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"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.imageswitchtest.MainActivity" >    <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"/>        <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center">        <ButtonAndroid:id= "@+id/btn_up"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Previous Picture"/>        <ButtonAndroid:id= "@+id/btn_next"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Next Picture"/>    </LinearLayout></LinearLayout>

  

  Example download links are:https://files.cnblogs.com/files/hmxin/ReadBookProject.zip

Android Small Example: Use the reflection mechanism to read pictures to make a picture browser

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.