Simple tutorial for Android-gun 9 (BitmapFactory. Options scaling resource images ),

Source: Internet
Author: User

Simple tutorial for Android-gun 9 (BitmapFactory. Options scaling resource images ),

We know that all the applications we write have certain memory limitations. If the program occupies too much memory, it is prone to OOM (OutOfMemory) exceptions. Therefore, when displaying a high-resolution image, it is best to compress the image first. The size of the compressed image should be similar to the size of the control used to display the image, so that both the Display Effect and memory usage can be taken into account.

The BitmapFactory. Options class has a field calledInJustDecodeBounds . The descriptions of this Member in the SDK are as follows:
If set to true, the decoder will return null (no bitmap), but the out...
That is to say, if we set it to true, then BitmapFactory. decodeFile (String path, Options opt) does not actually return a Bitmap to you. It only returns its width and height to you, so that it will not occupy too much memory, OOM will not happen so frequently.

The following example shows how to implement a thumbnail.

1. layout file:

<?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>


2. The MainActivity. java code is as follows:

Package org. yayun. demo; import java. io. inputStream; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. matrix; import android. OS. bundle; import android. widget. imageView; public class MainActivity extends Activity {private ImageView imageView1; private ImageView imageView2; Bitmap mBitmap; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); 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 ;}}


3. Run the instance:

The thumbnail effect is displayed.

If you like it, follow me! Thank you!

Related Article

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.