Android ImageView does not display JPEG images and how to reference picture resources in Android Studio

Source: Internet
Author: User

Android ImageView does not display JPEG pictures

Today in writing a small instance, ImageView in the XML set is invisible, in the code needs to be set to Setvisibility (view.visible), but the picture is not displayed, Images converted to PNG or other JPEG formats can be displayed correctly.

cause : The displayed picture size is 5.39K, the picture format is damaged, so it is not displayed properly.

fix : Change a picture, or regenerate a JPEG image.

If it is still not displayed properly, it is recommended that after you have set the visible, the following method is called:

Iv.setvisibility (view.visible); View.requestlayout ();
The something has changed which have invalidated the layout of this view. This would schedule a layout pass of the view tree.
We can understand the layout of the view again.

Use: Sometimes we may cause the display error after changing the content of a view, for example, when writing a ListView, reusing one of the TextView in the Convertview may cause an error in the display because the text length is different from the front and back. At this point we can call the Requestlayout method after changing the content to resolve.

recommendation : PNG is a lossless format and JPG is a lossy format. JPG in the processing of a lot of color pictures, depending on the compression rate, sometimes remove some of the naked eye to identify the small gap between the middle color. However, PNG will strictly retain all color numbers for this basic requirement. So. The size of the picture is large, or the number of colors, especially the gradual discoloration of many times, PNG volume will be significantly larger than JPG.

The Android interface can be used PNG preferably with PNG, because the 32-bit PNG color transitions smoothly and supports transparency. JPG is a pixel compressed picture, the quality has dropped, and then take to do 9patch buttons and the tension of the control will inevitably be miserable, to try to avoid. In fact, in the Res icon is used in PNG format, that is, Google recommends the use of PNG format pictures.

For the color of miscellaneous, such as photo wallpaper and other pictures (some applications like to engage in the start screen), which is best to use JPG, this image compression before compression of the naked eye to distinguish almost no, if the preservation of PNG volume will be jpg several times even dozens of times times, a serious waste of volume.

explanation : What is 9patch,patch's Chinese meaning is "piece, block" meaning, that here according to Chinese meaning is 9 or 9 pieces. So imagine this picture will be divided into 9 pieces, as shown in:


9patch only explains so much, you need to know more about it to continue in-depth research.


How to reference a picture resource in Android Studio

In Eclipse, place a. png or. jpg picture in the Drawable folder, such as DRAWABLE-HDPI,DRAWABLE-LDPI,DRAWABLE-MDPI,DRAWABLE-XHDPI,DRAWABLE-XXHDPI, based on the pixel size of the picture. The same picture is placed in a different drawable folder and will be displayed differently. But it is the same when referenced in XML: Android:[email protected]/pic or Android:[email protected]/pic.



But it's not the same in Android studio, and there are not so many drawable directories, but only one drawable folder. Instead, there are a lot of mipmap folders, such as mipmap-hdpi,mipmap-mdpi,mipmap-xhdpi,mipmap-xxhdpi. :


First try to put the picture in drawable, then use Android:[email protected]/pic or Android:[email protected]/pic in XML. Use, found no problem, can be displayed completely. But why are there so many mipmap?

Google's official explanation is performance optimization, the use of small memory and so on, in short mipmap this form is better than drawable. So, we recommend using Mipmap in Android Studio. Well, let's put the picture under the Mipmap folder, how do we use it in XML?

Android:[email Protected]/pic
Very similar to the way pictures are referenced in drawable, and also by using @. So this is also very simple, different pixels of the Mipmap folder pictures are the same use. But note: In as, the same picture is placed under the Drawable folder and placed under the Mipmap folder, the effect is not the same, in size, scale, pixels may vary. Compare Android Studio and Eclipse to learn that they are both similar and have a certain difference.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android ImageView does not display JPEG images and how to reference picture resources in Android Studio

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.