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 _