Discuss in detail the efficiency of Android reading image display from files

Because reading images from files to bitmap is a time-consuming thing, several feasible methods are studied and compared.

First, explain why it is time-consuming. This is because when reading a bitmap from a JPG or PNG file, first, it needs to operate on external storage, and the image file is generally large. Second, when creating a bitmap, it basically needs to operate on the original image, such as downsampling, clipping, rotation, etc. Therefore, how to efficiently read and present pictures is a problem worthy of study.

According to my ideas, I have come up with three schemes:

1. In the current UI thread, the image is directly read and manipulated, and then rendered.

2. Open a new sub thread to read and operate the image, and then use the serializable method in the bundle to return it to the UI thread and render it.

3. Other methods are the same as 2, but the related methods of parcel in bundle are used.

Method 1

The operation is very simple. First read out the size and other information of the picture file, then calculate its scaling according to its size, and cut out a part of the picture. Finally, the image is displayed in the ImageView space. The average consumption time is 72.75ms

Method 2

Start child thread

The operations in the child thread are basically the same as 1

Pass the bitmap back to the UI thread and render it

The average consumption time of method 2 was 83.93 Ms

Method 3

This method needs to create a new class to implement the Parcelable interface

Operations in child threads

The average consumption time of method 3 was 87.35 Ms

Result analysis

The three methods are tested on Meizu MX1 mobile phones. Theoretically, method 3 should be faster than method 2, but at least according to my experimental results, when transmitting a small amount of data (the image is about a few MB or hundreds of KB), the time-consuming of data transmission is not the key. The time-consuming of the two methods is almost the same. Method 1 does not use data transfer between threads, so it takes the least time.

Therefore, I conclude as follows:

1. If you have to wait until the image is loaded to allow users to operate, you can directly operate the image in the UI thread. At this time, you can add a ProgressDialog to prompt that the image is being loaded.

2. If you need to load images while allowing users to operate, you should open a new sub thread. However, when the amount of data is small, there is little difference between serializable and Parcelable.

3. In short, when the size and number of images are small, you can directly read images in the UI thread, but it is better to open sub threads when the size and number of images are large.

The above article discusses in detail the efficiency of Android reading image display from files, which is all the content shared by Xiaobian. I hope it can give you a reference and support more 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
分享
二维码
< <上一篇
下一篇>>