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!