Android screen self-adaptation

Source: Internet
Author: User

 

This is a serious problem that I recently encountered and cannot be solved perfectly. In desperation, I posted a question to the csdn Android version. I did not expect to disturb the moderator and gave me a lot of help, so that I can solve similar problems perfectly. I decided to wait for the csdn Android forum. Haha.

 

Problem description:

On the WVGA 854 device, the adaptive change of image size leads to incorrect results. In my project, the image will always be reduced.

For example, when an 8-54x90 image is adaptive, the image width and height will be reduced by 1.5 times, which will prevent full screen display.

 

Problem Analysis:

I found that the image read is normal, which is 854x90, and the density 240 is the same as the real machine. However, after a code sentence, the density becomes 160, the height, and the width is reduced by 1.5 times. This code is bitmapdrawable drawable = new bitmapdrawable (Bitmap );

 

Soon I found that this is because the density has changed. Android will automatically adapt the image according to the density, from 240 to 160, which is exactly 1.5 times the width, the height is also reduced by 1.5 times.

 

So I'm sure the problem is density, and it turns out that my judgment is correct.

 

Solution:

After reading the android development documentation, I found that the feature was added from Android 1.6 to adapt to changes based on density. The API of our project uses Android 1.5.

1.5 does not provide the corresponding API to adapt to the real machine's density, but uses a default density 160. this explains why density changes to 160 every time I pass bitmapdrawable.

 

In the 1.6 Documentation, the APIs used in our project have been deprected, and the density-based APIs are provided.

 

This is the constructor used in our project.

Bitmapdrawable

(Bitmap
Bitmap)


This constructor is deprecated.
UseBitmapDrawable(Resources, Bitmap)

To ensure
That the drawable has correctly set its target density.

 

After reading the API Doc, I used the following constructor:

 

Bitmapdrawable

(Resources
Res, bitmap
Bitmap)

Create drawable from a bitmap, setting initial target density based on
The display metrics of the resources.

 

This API description clearly states that the initial target density will be set based on the real machine's density.

In this way, bitmapdrawable automatically changes the image based on the correct density.

 

The first parameter in the constructor is easily obtained. You can call activity. getresources () directly to obtain the parameter. The second parameter is our original map.

 

In this case, the problem is successfully solved.

 

If you have any questions, leave a message.

 

 

 

 

 

 

 

 

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.