Android Notes (28) Simple picture use of Android pictures

Source: Internet
Author: User

User interface to a large extent determines whether the app is received by users, in order to provide a friendly interface, you need to use the image in the application, Android provides a wealth of image processing capabilities.

Simple use of images using Drawable objects

After adding drawable resources to Android apps, The system automatically creates an index entry in the R.java file: R.drawable.filename, and in Java you can get the index of the resource (a constant of type int) through R.drawable.filename, and if you want to get the actual drawable object, you can call the The getdrawable (int id) method of the resources to get.

ImageView image = (ImageView) Findviewbyid (r.id.image); Image.setimageresource (R.DRAWABLE.PIC1);
Bitmap and Bitmapfactory

Bitmap represents a bitmap, and the picture encapsulated in Bitmapdrawable is a bitmap object. In order to wrap a bitmap object into a Bitmapdrawable object, the developer can call the Bitmapdrawable constructor:

New Bitmapdrawable (bitmap);

If you need to get the bitmap object wrapped by bitmapdrawable, you can call Bitmapdrawable's Getbitmap () method

Bitmap Bitmap = Drawable.getbitmap ();

Bitmapfactory is a tool class for parsing and creating bitmap objects from different data sources

Bitmapfactory provides a series of methods to help us create a bitmap object, which we can then

Imageview.setimagebitmap (Bitmap BM)

To change the image displayed by a imageview.

Because the system content is relatively small, if the system constantly to parse, create bitmap objects, there may be memory overflow error, so Android for Bitmap provides two methods to determine whether it has been recycled, if not, forcing bitmap to reclaim their own

    Boolean isRecycled ();    Determines whether the bitmap object has been reclaimed    by void recycle ()    forcing the bitmap object to reclaim itself

An example:

 Packagecn.lixyz.bitmaptest;Importandroid.app.Activity;ImportAndroid.content.res.AssetManager;Importandroid.graphics.BitmapFactory;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList; Public classMainactivityextendsActivityImplementsView.onclicklistener {PrivateImageView ImageView; PrivateArraylist<string>images; PrivateButton Btnnext; PrivateButton Btnlast; Private intindex = 0; PrivateAssetmanager am;  Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Get ComponentsImageView =(ImageView) Findviewbyid (r.id.image); Btnnext=(Button) Findviewbyid (R.id.next); Btnlast=(Button) Findviewbyid (r.id.last); //call the GetImages method to get the collection of pictures under assetsgetimages (); //Click the buttonBtnnext.setonclicklistener ( This); Btnlast.setonclicklistener ( This); }    /*** Because assets not only have pictures, there will be other directories or files, you need to identify the image to be stored in a list as a data source*/     Public voidgetimages () {string[] Tmpimgs=NULL; Images=NewArraylist<string>(); //getassets () method to get Assetmanager objectAM =getassets (); Try {            //Get Asset Content listTmpimgs = Am.list (""); //pick out a. jpg file and save it in list             for(inti = 0; i < tmpimgs.length; i++) {                if(Tmpimgs[i].endswith (". jpg") {images.add (tmpimgs[i]); }            }        } Catch(IOException e) {e.printstacktrace (); }    }    /*** Click on the button event * *@paramv View object, used to determine what button was clicked*/@Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.next:Try{Index++;//Subscript +1, for displaying the next picture                    if(Index >= images.size ()) {//prevent cross-borderindex = 0; }                    //determine if bitmap has been recycled, and if not, recycle firstbitmapdrawable BD =(bitmapdrawable) imageview.getdrawable (); if(BD! =NULL&&!Bd.getbitmap (). isRecycled ())                    {Bd.getbitmap (). Recycle (); }                    //The Open method of the Assetmanager class, which can return an input streamInputStream is =Am.open (Images.get (index)); //change the display image by Bitmapfactory's Decodestream () methodImageview.setimagebitmap (Bitmapfactory.decodestream (IS)); } Catch(IOException e) {e.printstacktrace (); }                 Break;  CaseR.id.last:Try{Index--;//Subscript +1, for displaying the next picture                    if(Index < 0) {//prevent cross-borderindex = Images.size ()-1; }                    //determine if bitmap has been recycled, and if not, recycle firstbitmapdrawable BD =(bitmapdrawable) imageview.getdrawable (); if(BD! =NULL&&!Bd.getbitmap (). isRecycled ())                    {Bd.getbitmap (). Recycle (); }                    //The Open method of the Assetmanager class, which can return an input streamInputStream is =Am.open (Images.get (index)); //change the display image by Bitmapfactory's Decodestream () methodImageview.setimagebitmap (Bitmapfactory.decodestream (IS)); } Catch(IOException e) {e.printstacktrace (); }                 Break; }    }}
Mainactivity.java
<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"android:orientation= "vertical"Tools:context=". Mainactivity ">    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:gravity= "Center">        <ImageViewAndroid:id= "@+id/image"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_gravity= "Center" />    </LinearLayout>    <LinearLayoutAndroid:id= "@+id/buttons"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_alignparentbottom= "true"android:layout_gravity= "Bottom"android:orientation= "Horizontal">        <ButtonAndroid:id= "@+id/last"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Previous" />        <ButtonAndroid:id= "@+id/next"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1"Android:text= "Next" />    </LinearLayout></Relativelayout>
Activity_main.xml

Operation Result:

Android Notes (28) Simple picture use of Android pictures

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.