When the width of bitmap is greater than the maximum display width of imageview, the width and height of imageview are recalculated to adapt to the width and height of Bitmap.

Source: Internet
Author: User

When bitmap is larger than the width of the imageview display control, if you do not recalculate the width and height of the imageview, the height of the imageview will exceed the height of the displayed image, and the user experience will be poor, therefore, we need to recalculate the width and height of the display control. Here we assume that the maximum width of imageview is the screen width. The following tool class can be used to solve the problem:

Android gets the screen width and height:
// Windowmanager manage = getwindowmanager ();
// Display = manage. getdefadisplay display ();
// Screenheight = display. getheight ();
// Screenwidth = display. getwidth ();

Inputstream is = getresources (). openrawresource (R. drawable. Sy );

Bitmap mbitmap = bitmapfactory. decodestream (is );
Linearlayout. layoutparams Lp = getlayoutparams (mbitmap, screenwidth );
Imageview1.setlayoutparams (LP );
Imageview1.setimagebitmap (mbitmap );

 

 

Public static linearlayout. layoutparams getlayoutparams (Bitmap bitmap, int screenwidth ){
Float rawwidth = bitmap. getwidth ();
Float rawheight = bitmap. getheight ();
Float width = 0;
Float Height = 0;
Log. I ("hello", "Original Image Height:" + rawheight + "Original Image Width:" + rawwidth );
Log. I ("hello", "original aspect ratio:" + (rawheight/rawwidth ));
If (rawwidth> screenwidth ){
Height = (rawheight/rawwidth) * screenwidth;
Width = screenwidth;
} Else {
Width = rawwidth;
Height = rawheight;
}
Log. I ("hello", "processed Image Height:" + height + "processed image width:" + width );
Linearlayout. layoutparams = new linearlayout. layoutparams (INT) width, (INT) height );
Return layoutparams;
}

When the width of bitmap is greater than the maximum display width of imageview, the width and height of imageview are recalculated to adapt to the width and height of Bitmap)

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.