Learning android to scale resource images (27 ),

Source: Internet
Author: User

Learning android to scale resource images (27 ),

:



When loading images, I will use SetImageBitmap, setImageResource, BitmapFactory. decodeResource to set a picture.


When you use the preceding method to set images, the createBitmap on the Java layer will be used. This will consume a lot of memory, which may easily lead


OOM (Out Of Memory), so we recommend using the BitmapFactory. Options class to set a resource map.


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 image mBitmap = readBitMap (); // scale the resource image imageView2.setImageBitmap (zoomBitmap (mBitmap, mBitmap. getWidth ()/4, mBitmap. getHeight ()/4);}/*** read resource image * @ return */private Bitmap readBitMap () {BitmapFactory. options opt = new BitmapFactory. options ();/** sets the decoder to be decoded in the best way */opt. inPreferredConfig = Bitmap. config. RGB_565; // use opt in combination with the following two fields. inPurgeable = true; opt. ininputtransferable = true;/** get resource image */InputStream is = this. getResources (). openRawResource (R. drawable. mei); return BitmapFactory. decodeStream (is, null, opt);}/*** zoom image * @ 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 ); /** scale by using the postScale method of the Matrix class */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:orientation="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>



Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44281087 sentiment control _





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.