Solution to the problem that Android ImageView does not display JPEG images

Android ImageView does not display JPEG images

Today, I am writing a small example. ImageView is set as invisible in XML. It needs to be set as setvisibility (view. Visible) in the code, but the picture is not displayed. Pictures in PNG or other JPEG format can be displayed normally.

Cause: the size of the displayed picture is 5.39k, and the picture format is damaged, so it cannot be displayed normally.

Solution: change a picture, or regenerate a JPEG picture.

If it is still not normal, it is recommended that after VISIBLE is set, the following methods are called:

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.

We can understand it as a rearrangement of the view.

Usage: sometimes we may change the contents of a view to cause errors in the display. For example, when we write ListView, we reuse a TextView in convertview. It may cause errors in the display due to the different length of text before and after filling. At this point, we can call the requestLayout method after changing the content.

Suggestion: PNG is a lossless format and JPG is a lossy format. When dealing with pictures with many colors, JPG sometimes removes some intermediate colors with small visual recognition gap according to different compression rates. However, PNG will strictly retain all color numbers for the basic requirement of lossless. So. When the picture size is large, or the number of colors is large, especially when there are many gradients, the volume of PNG will be significantly larger than that of jpg.

The Android interface can use PNG, preferably PNG, because the 32-bit PNG color transition is smooth and transparent. Jpg is a pixel compressed picture, and the quality has decreased. It must be terrible to use it as the button of 9patch and the control of tile stretching. It should be avoided as much as possible. In fact, under res, icons are in PNG format, which means that Google recommends PNG format images.

For pictures with complex colors, such as photos and wallpaper (the startup screen of some applications likes to do this), JPG is the best. The naked eye resolution of this kind of picture is almost negligible before compression. If it is saved as PNG, the volume will be several times or even dozens of times that of jpg, which is a serious waste of volume.

Explanation: what is 9patch? Patch means "slice, block" in Chinese. In Chinese, it means 9 slices or 9 pieces. Therefore, it is conceivable that this picture will be divided into 9 pieces, as shown in the figure below:

9patch only explains so many things that you need to know in detail. You can continue to study them in depth.

How to reference picture resources in Android studio

In eclipse, according to the pixel size of the picture, put the. PNG or. JPG picture in the drawable folder, such as drawable hdpi, drawable LDPI, drawable MDPI, drawable xhdpi, drawable xxhdpi. When the same picture is placed in different drawable folders, it will be displayed differently. But it is the same when referenced in XML: Android: SRC = @ drawable / pic or Android: background = @ drawable / pic.

But it's different in Android studio. I found that there are not so many drawable directories, but only one drawable folder. Instead, there are many mipmap folders, such as mipmap hdpi, mipmap MDPI, mipmap xhdpi, and mipmap xxhdpi. As shown in the figure:

First try to put the picture in drawable, and then use Android: SRC = @ drawable / pic or Android: background = @ drawable / pic in XML. After use, no problem is found. It can be displayed completely. But why are there so many mipmaps?

Google's official explanation is performance optimization, small memory consumption, etc. in short, mipmap is better than drawable. So, we recommend using mipmap in Android studio. Well, let's put the pictures in the mipmap folder. How can we use them in XML?

android:src=@mipmap/pic

It is very similar to the way pictures are referenced in drawable, and @ is also used for reference. So this is also very simple. The mipmap folder images of different pixels are used the same. But note: in as, the display effect of the same picture in drawable folder is different from that in mipmap folder. There may be differences in size, scaling and pixels. Compare Android studio with eclipse. They are both similar and different.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>