:
I use Setimagebitmap, Setimageresource, Bitmapfactory.decoderesource to set a picture when I load my pictures normally.
When the film is set by the above method, it will be done through the Java layer's createbitmap, which will consume a lot of memory and easily cause
OOM (out of Memory), it is recommended to use the Bitmapfactory.options class to set a resource graph.
See the following code:
public class Mainactivity extends Activity {private ImageView imageview1;private ImageView imageView2; Bitmap mbitmap; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.image); Initview ();} private void Initview () {imageview1= (ImageView) Findviewbyid (r.id.imageview1); imageview2= (ImageView) Findviewbyid ( R.ID.IMAGEVIEW2);//Read the resource Picture Mbitmap=readbitmap ();//Zoom the resource Picture Imageview2.setimagebitmap (Zoombitmap (Mbitmap, Mbitmap.getwidth ()/4, Mbitmap.getheight ()/4));} /** * Read Resource picture * @return */private Bitmap readbitmap () {bitmapfactory.options opt=new bitmapfactory.options ();/* * Set to allow decoder to best Mode decoding */opt.inpreferredconfig=bitmap.config.rgb_565;//The following two fields need to be combined using opt.inpurgeable=true;opt.ininputshareable=true ;/* * Get resource Picture */inputstream is=this.getresources (). Openrawresource (R.drawable.mei); return Bitmapfactory.decodestream (IS, null, opt);} /** * Zoom Picture * @param bitmap * @param w * @param h * @return */public bitmap zoombitmap (bitmap bitmap, int w, int h) {iNT width = bitmap.getwidth (); int height = bitmap.getheight (); Matrix matrix = new Matrix (), Float scalewidht = ((float) w/width), Float ScaleHeight = ((float) h/height);/* * via Matrix The Postscale method of the class is scaled */matrix.postscale (SCALEWIDHT, ScaleHeight); Bitmap newbmp = Bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, true); return newbmp;}}
Image.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <imageview android:id= "@+id/imageview1" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content " android:src=" @drawable/mei "/> <imageview Android:id= "@+id/imageview2" android:layout_width= "wrap_content" android:layout_below= "@+id/ ImageView1 " android:layout_height=" wrap_content " android:layout_margintop=" 10DP " android:src=" @ Drawable/mei "/></relativelayout>
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/44281087 Emotional Control _
Learn about Android by scaling the resource images (27)