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 that are converted to PNG or other JPEG formats are displayed correctly.

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

Solution : Change a picture. or create a JPEG picture again.

Assuming that the display is still not working properly, it is recommended that after the visible is set, the following methods are called, for example:

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 Convertview may cause an error in the display due to the different length of text that is filled in and out. 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 very many colors of the picture, 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 the color numbers for the basic requirement of lossless. So. Large picture size. or the number of colors, especially the gradual discoloration of many times. PNG's volume is 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 do the 9patch button and the tension control must be dire to avoid. In fact, the icon in RES is in PNG format. This means that Google recommends using a PNG-formatted image.

For the color complex. For example, pictures such as photo wallpaper (some of the app's splash screen like this). It's better to use JPG than that. Such a picture compressed before compression after the naked eye to distinguish almost negligible. Assuming that the PNG volume is saved will be several times even dozens of times times the JPG, a serious waste of volume.

explanation : what is 9patch. The Chinese meaning of patch is "film." The meaning of the block ". It means 9 or 9 pieces in Chinese terms. So it is conceivable that this image will be divided into 9 pieces, as seen in the following:


9patch only explains so much. A specific understanding of the need to continue in-depth study.


How to reference a picture resource in Android Studio

In Eclipse, place a. png or. jpg image in the drawable directory, such as DRAWABLE-HDPI,DRAWABLE-LDPI, based on the pixel size of the image. drawable-mdpi,drawable-xhdpi. drawable-xxhdpi. The same picture is placed in a different drawable directory. The display will be different. But it is the same when referenced in XML: Android:[email protected]/pic or Android:[email protected]/pic.



However, in Android Studio it is not the same, found that there are not so many drawable directory, but only a drawable directory. Instead, there are very many mipmap directories, such as mipmap-hdpi,mipmap-mdpi,mipmap-xhdpi,mipmap-xxhdpi. What you see:


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. Fully capable of displaying.

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 that you use Mipmap in Android Studio. Well, let's put the picture in the Mipmap directory. So how do you use it in XML?

Android:[email Protected]/pic
This is similar to the way images are referenced in drawable, and also by using @.

So it's easy too. Mipmap catalog images of different pixels are used in the same way. Note: In the AS, the same picture is placed in the drawable directory and placed in the Mipmap directory to display the effect is not the same. May vary in size, scale, and pixels.

Comparing Android Studio to Eclipse, they are both similar and somewhat different.

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.