Learn about Android by scaling the resource images (27)

Source: Internet
Author: User



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)

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.