1. Drawable Object
After the Android app has added the Drawabe resource, the Android SDK creates an index entry in the R manifest file for this resource file: R.drawable.file_name, which we can then use in the XML resource file @drawable/file_ Name to access the Drawable object, you can also access the Drawable object through R.drawable.file_name in Java code, and R.drawable.file_name in Java code is just a constant of type int, It only represents an ID for the Drawable object, and if you want to get the actual drawable object, you need to call the getdrawable (int id) method of the resources to get it.
2, Bitmap and Bitmapfactory
(1the bitmap represents a bitmap, and the image in Bitmapdrawable is a bitmap object that the developer can call bitmapdrawable to encapsulate a bitmap object as a Bitmapdrawable object: Wraps a Bitmap object into a Bitmapdrawable object bitmapdrawable drawable=Newbitmapdrawable (bitmap); (2If you need to get the bitmap object wrapped by bitmapdrawable, you can call Bitmapdrawable's Getbitmap () method: Gets the Bitmapdrawable object that the bitmap object wraps Bitma P Bitmap=Drawable.getbitmap (); (3Bitmap provides a number of static methods to create a new Bitmap object: Bitmap.createbitmap (source, x, Y, Width,height): from the specified coordinate point x, y of source bitmap sources, to extract the width Widt H, a section of high height, creates a new Bitmap Bitmap.createscaledbitmap (SRC, dstwidth, dstheight,filter): Scales the source bitmap src, shrinks the width dstwidth, High Dstheight new Bitmap bitmap.createbitmap (width,height, config): Creates a new bitmap with wide width and height height bitmap.createbitmap (source , x, y,width, Height, m,filter): Starts with X, y, the specified coordinate point of source bitmap sources, digs a wide width, high height area, creates a new bitmap, and transforms according to the rules specified by the matrix ( c1>4Bitmapfactory is a tool class that provides methods for parsing and creating bitmap objects from different data sources: Bitmapfactory.decodebytearray (byte[] Data,intOffsetintlength): Parses byte data of length to bitmap object Bitmapfactory.decodefile (String pathName) starting from the offset position of the specified byte array: Resolves, creates bitmap object Bitmapfactory.decodefiledescriptor (FileDescriptor fd) from the file specified by pathname: Used to parse from FileDescriptor file, Create Bitmap Object Bitmapfactory.decoderesource (Resources res,intID): Used to parse from the specified resource file, create a Bitmap object Bitmapfactory.decodestream (InputStream is) from the given resource ID: used to parse, create bitmap objects from the specified input stream (5Android provides two ways for bitmap to determine if it has been recycled, and to force bitmap to reclaim itself isRecycled (): Determine if the bitmap object has been reclaimed recycle (): Force a Bitmap object to reclaim itself immediately /c3>
3, Example: Picture Viewer, Query assets directory of pictures
Layout file:
<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:gravity= "Center" > <ImageViewAndroid:id= "@+id/imageview"Android:layout_width= "300DP"Android:layout_height= "300DP" /> <ButtonAndroid:id= "@+id/btn"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Search Next" /></LinearLayout>
Activity file:
PackageCom.example.image;Importjava.io.IOException;ImportJava.io.InputStream;Importandroid.app.Activity;ImportAndroid.content.res.AssetManager;Importandroid.graphics.BitmapFactory;Importandroid.graphics.drawable.BitmapDrawable;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView;/*** Bitmap and Bitmapfactory *@authorYinbenyang*/ Public classMainactivityextendsActivity {PrivateImageView ImageView; string[] Images=NULL; PrivateButton btn; //for managing resources under the Assets folderAssetmanager assets =NULL; //Current Picture intcurrentimg = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView=(ImageView) Findviewbyid (R.id.imageview); BTN=(Button) Findviewbyid (R.ID.BTN); Assets=getassets (); Try { //get all files under the/assets directoryImages = Assets.list (""); } Catch(IOException e) {e.printstacktrace (); } //Click the button to view the next pictureBtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//If the picture is the last one, set the first one if(Currentimg >=images.length) {currentimg= 0; } //find the next picture file while(!images[currentimg].endswith (". png") &&!images[currentimg].endswith (". jpg") &&!images[currentimg].endswith (". gif") ) {currentimg++; if(Currentimg >=images.length) {currentimg= 0; }} InputStream Assetfile=NULL; Try { //opens the input stream corresponding to the specified resourceAssetfile = Assets.open (images[currentimg++]); } Catch(IOException e) {e.printstacktrace (); } bitmapdrawable bitdrawable=(bitmapdrawable) imageview.getdrawable (); //If the picture is not recycled, force the image to be recycled first if(Bitdrawable! =NULL&&!Bitdrawable.getbitmap (). isRecycled ()) {Bitdrawable.getbitmap (). Recycle (); } //change the picture displayed by ImageViewImageview.setimagebitmap (Bitmapfactory.decodestream (assetfile)); } }); }}
The example effect is as follows: Click Search Next, rotate the picture
Android Image and image processing series (I, Bitmap and bitmapfactory)