Image adaptation screen problems in Android,

Source: Internet
Author: User

Image adaptation screen problems in Android,

I recently learned how to beautify the android APP interface. This problem occurs when I make an APP startup screen: a picture I like very much is inconsistent with the screen resolution of my mobile phone !! What a pain! As a result, there are many methods for searching on the internet, basically all of which have been tried, and the results are not very good. Let's talk about the answers from many people on the Internet.

One of the methods is to add setScaleType in layout. You can set several values, which are described below:

1. ImageView. ScaleType. CENTER. The image is displayed in the center of the View according to the image size. If the image size is greater than the screen size, the middle part is displayed.

2. ImageView. ScaleType. CENTER_CROP. Scaled up, the image is still displayed in the center, so that the image width or height is equal to or equal to the side length of the View.

3. ImageView. ScaleType. CENTER_INSIDE. Scale down proportionally. The effect is the opposite of the above.

4. ImageView. ScaleType. FIT_CENTER. Scale up or down an image to the width of the View in proportion and display it in the center.

5. FIT_START and FIT_END have the same scaling effect as FIT_CENTER on the image, but they are displayed in different positions. FIT_START is placed on the top, FIT_CENTER is centered, and FIT_END is placed on the bottom.

6. FIT_XY. Scale the image proportionally. The goal is to fill the image with the entire View. This method enables the image to be displayed in full screen.

But !!! The above method will cut the image or directly deform the image, so the final effect is very poor. Below is:

Then I found a method written by a blogger to maximize the image scaling and ensure that the image is not deformed while filling the screen. Paste the code below:

Private void setBsetBitmap (Bitmap bitmap, ImageView imageView, int width,
Int height ){
// Calculate the Optimal Scaling Factor
// Float scaleX = (float) width/bitmap. getWidth ();
// Float scaleY = (float) height/bitmap. getHeight ();
// Float bestScale = scaleX <scaleY? ScaleX: scaleY;
// Calculate the optimal scaling factor, with the fill width and height as the target
Float scaleX = (float) width/bitmap. getWidth ();
Float scaleY = (float) height/bitmap. getHeight ();
Float bestScale = scaleX> scaleY? ScaleX: scaleY;
// Calculate the optimal zoom factor with the padding height
// Float bestScale = (float) height/bitmap. getHeight ();

Float subX = (width-bitmap. getWidth () * bestScale)/2;
Float subY = (height-bitmap. getHeight () * bestScale)/2;

Matrix imgMatrix = new Matrix ();
ImageView. setScaleType (ScaleType. MATRIX );
// Scale the optimal size
ImgMatrix. postScale (bestScale, bestScale );
// Move to the center position for display
ImgMatrix. postTranslate (subX, subY );
// Set the Matrix
ImageView. setImageMatrix (imgMatrix );

ImageView. setImageBitmap (bitmap );
}

Parameter: bitmap: indicates the image to be displayed. imageView: Controls for displaying images; width: width of the screen; height: screen height. The screen width and height can be obtained using the following methods:

WindowManager windowManager = getWindowManager ();
Display display = windowManager. getDefaultDisplay ();
ScreenWidth = display. getWidth ();
ScreenHeight = display. getHeight ();

You can call this method on the Image Display page to make the image fit the screen without deformation. Of course, this method is different from the image display method suitable for screen resolution. After all, it is not the source image, after a certain proportion of adjustment, but in these methods, the effect of this method is the best. Below is:

Although a part of the image has been cut off, it looks much better. Because the size of the image is much higher than that of the mobile phone, the difference will be larger, if the size is the same as that of the mobile phone screen, the difference between the size and the original image is very subtle.

Another method is to set the aspect ratio in layout and setadjustViewBounds (which can also be completed in code). Note that maxHeight and maxWidth need to be set in this method, at the same time, setting layout_height and layout_width to wrap_content can achieve the same effect, but using this method on mobile phones of different sizes still has different effects, so it is better to write a setBsetBitmap method, of course, if you have images that adapt to the screen, you don't need to bother.

This is the first time a newbie has written a blog and has just been learning Android for a long time. please correct me if you have any mistakes!

Reference: http://blog.csdn.net/larryl2003/article/details/6919513

Http://www.apkbus.com/android-178800-1-1.html


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.