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

Source: Internet
Author: User

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

Android ImageView does not display JPEG images

I am writing a small instance today. ImageView is set to INVISIBLE in xml, and it needs to be set to setVisibility (View. but the image is not displayed. If you change to PNG or another JPEG format, the image can be displayed normally.

Cause: the size of the displayed image is 5.39 kb, and the image format is damaged. Therefore, the image cannot be displayed normally.

Solution: change an image or regenerate a JPEG image.

If it still cannot be displayed normally, we recommend that you call the following method after setting VISIBLE:

iv.setVisibility(View.VISIBLE);view.requestLayout();
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 re-layout the view.

Purpose: Sometimes a Display error may occur after we change the content of a view, for example, when writing a ListView, reusing a TextView in convertview may cause an error in display because the length of text entered before and after it is different. At this time, we can call the requestLayout Method to Solve the Problem after changing the content.

Suggestion: png is a lossless format and JPG is a Lossy format. When processing images with many colors, JPG sometimes removes some intermediate colors with small differences between the human eyes Based on the compression ratio. However, PNG strictly retains the number of colors for Lossless purposes. So. When the image size is large, or the number of colors is large, especially when the color gradient is large, the size of the PNG will be significantly larger than that of the JPG.

The Android UI can use png, preferably png, because the 32-bit png color transition is smooth and supports transparency. Jpg is a pixel-compressed image, and the quality has been reduced. The button for making a 9patch and the control for stretching the Paster must be miserable and should be avoided as much as possible. In fact, icons in res are all in png format, which means Google recommends png images.

For pictures with complicated colors, such as photo wallpapers (some app startup pictures like this), it is best to use jpg, this image is almost invisible to the naked eye after compression. If it is saved as png, it will be several or even dozens of times the size of jpg, which seriously wastes the size.

Explanation: What is patch 9? The Chinese meaning of Patch is "piece, block". Here, 9 or 9 pieces are in Chinese. Therefore, we can imagine that this image will be divided into nine parts, as shown in:


9patch only explains so much. If you need more details, you can continue to study it.


How to reference image resources in Android Studio

In eclipse.jpg, put the. PNG or. jpg image in the drawable folder, such as drawable-hdpi, drawable-ldpi, drawable-mdpi, drawable-xhdpi, and drawable-xxhdpi. The same image is placed in different drawable folders and displayed differently. But it is the same when referenced in xml: android: src = @ drawable/pic or android: background = @ drawable/pic.



But it is different in Android Studio. There are not so many drawable directories, but only one drawable folder. Instead, many mipmap folders are displayed, such as mipmap-hdpi, mipmap-mdpi, mipmap-xhdpi, and mipmap-xxhdpi. :


First try to put the image in drawable, and then use android: src = @ drawable/pic or android: background = @ drawable/pic in xml. Use, found no problem, can be displayed completely. But why are there so many mipmaps?

The official explanation of Google is performance optimization and memory usage. In short, the form of mipmap is better than drawable. So, we recommend that you use mipmap in Android Studio. Well, we put the image under the mipmap folder. How can we use it in xml?

android:src=@mipmap/pic
It is similar to the reference method in which an image is placed in drawable. It is also referenced. Therefore, this is also very simple. The image in the mipmap folder of different pixels is used in the same way. However, note: In AS, the display effect of the same image in the drawable folder is different from that in the mipmap folder, and may vary in size, scaling, and pixels. Compared Android Studio and Eclipse, they are similar and different.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.