Android is compatible with the actual size of images obtained from tablets.

Source: Internet
Author: User

There is a picture in the project that shows a very small reason in the tablet. I have been suffering from failure to find the cause, and I have not tested it on the tablet. Today I found a resolution change, after writing the relevant method, record the comparison:
So that no wheels will be created in the future.
It is mainly used to obtain the image display problems related to the article. The getIntrinsicWidth () method is used to obtain the image. getIntrinsicWidth () is not the actual width of the image. It can be seen on the mobile phone without the difference being too far away, but on a tablet,
It is simply a little girl, so shy that the display is so small.
Back to the topic:
Solution:
1. Obtain the resolution of the mobile phone. Multiply the resolution by its density.

If (file. exists () {// if the file already exists, return Drawable drawable = Drawable. createFromPath (savePath); DisplayMetrics dm = new DisplayMetrics (); // get the cell phone screen resolution dm = context. getResources (). getDisplayMetrics (); // actual screen size // int screenWidth = dm. widthPixels; // int screenHeight = dm. heightPixels; float density = dm. density; // The actual image size int imgWidth = (int) (drawable. getIntrinsicWidth () * density); int imgHeight = (int) (drawable. getIntrinsicHeight () * density); // drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); drawable. setBounds (0, 0, imgWidth, imgHeight); //} return drawable ;}


Load when no image is available

Private void setDrawable (Drawable nDrawable) {drawable = nDrawable; // drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); // setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); DisplayMetrics dm = new DisplayMetrics (); // get the cell phone screen resolution dm = context. getResources (). getDisplayMetrics (); // actual screen size // int screenWidth = dm. widthPixels; // int screenHeight = dm. heightPixels; float density = dm. density; // The actual image size int imgWidth = (int) (drawable. getIntrinsicWidth () * density); int imgHeight = (int) (drawable. getIntrinsicHeight () * density); // drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); drawable. setBounds (0, 0, imgWidth, imgHeight); setBounds (0, 0, imgWidth, imgHeight); // note that if you add this parameter, the image and text will be confused}
Address: http://hongshengpeng.com/article/show/254.aspx

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.