Scaling of multimedia display images in Android

Source: Internet
Author: User

 

I. Image OOM exception: Sample Code:
public class MainActivity extends Activity {private ImageView iv_imageView;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_imageView = (ImageView) findViewById(R.id.Iv_imageView);}public void load(View view){Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/1.jpg");iv_imageView.setImageBitmap(bitmap);}}

Problem:

5038848,

20155392 Byte ---> 20155392/1024 = 19683KB ---> 19638/1024 = 19.2216796875MB19.2216796875MB

6.

Ii. Image Scaling Algorithm:

1.

2.

Code details:

Public class MainActivity extends Activity {private ImageView iv_imageView; private int running wwidth; private int running wheight; @ SuppressWarnings ("deprecation") @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); iv_imageView = (ImageView) findViewById (R. id. iv_imageView); // 1. get the resolution width and height of the mobile phone screen Display = getWindowManager (). getdefadisplay display (); required wwidth = display. getWidth (); returns wheight = display. getHeight (); System. out. println ("screen width:" + shadowwheight); System. out. println ("screen height:" + windowHeight);} public void load (View view) {// Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/1.jpg"); // iv_imageView.setImageBitmap (bitmap); Options opts = new Options (); // 2.0 sets the parser not to parse the bitmap, instead, it parses the out output (width, height) of the image and does not apply for an internal opts space for each vertex of the image. inJustDecodeBounds = true; // 2. obtain the image width and height information BitmapFactory. decodeFile ("/sdcard/1.jpg", opts); int picHeight = opts. outHeight; int picWidth = opts. outWidth; System. out. println ("Image Width:" + picWidth); System. out. println ("Image Height:" + picHeight); // 3. int scaleX = picWidth/20.wwidth; int scaleY = picHeight/windowHeight; System. out. println ("horizontal scaling:" + scaleX); System. out. println ("vertical scaling ratio:" + scaleY); // 3.2. determine the scaling ratio: int scale = 1; if (scaleX> scaleY & scaleY> 1) {scale = scaleX;} if (scaleY> scaleX & scaleX> 1) {scale = scaleY;} System. out. println ("final scaled ratio:" + scale); // 4. actually parse this image opts. inJustDecodeBounds = false; opts. inSampleSize = scale; Bitmap bitmap = BitmapFactory. decodeFile ("/sdcard/1.jpg", opts); iv_imageView.setImageBitmap (bitmap );}}
Runtime log:

09-29 16:05:37. 715: I/System. out (23786 ):

09-29 16:05:37. 715: I/System. out (23786 ):

09-29 16:07:12. 085: I/System. out (23786 ):

09-29 16:07:12. 085: I/System. out (23786 ):

09-29 16:07:12. 105: I/System. out (23786 ):

09-29 16:07:12. 115: I/System. out (23786 ):

09-29 16:07:12. 115: I/System. out (23786 ):

 

 

09-29 16:17:26. 475: I/System. out (23786 ):

09-29 16:17:26. 475: I/System. out (23786 ):

09-29 16:17:33. 336: I/System. out (23786 ):

09-29 16:17:33. 336: I/System. out (23786 ):

09-29 16:17:33. 345: I/System. out (23786 ):

09-29 16:17:33. 345: I/System. out (23786 ):

09-29 16:17:33. 345: I/System. out (23786 ):

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.