Share a lightweight image loading class imageloader

Image loaders are a big push on the Internet, such as nostra13 image loader, xutil and Facebook fresco. Many, but with a learning attitude, I recently wrote a lightweight (local) image cache loading function to share with you.

It involves lrucache, executorservice, bitmapfactory principle for processing large pictures, and view setTag() .

Well, I won't say much. Let's take a step-by-step approach: first, let's see how to use the class I encapsulated:

Isn't it simple.

Next, analyze the imageloader class:

We all know that the memory of the mobile phone is limited and it is impossible to add all pictures into the memory. Therefore, Android provides an lrucache method. The algorithm used is: the least recently used algorithm, and the least used pictures are constantly added to the cache, and the least used pictures are constantly removing the cache, so as to avoid the problem of insufficient memory.

The initialization code of lrucache is as follows:

Like HashMap, lrucache uses put and get to get cached things.

Then look at the specific loading of the picture and paste the code first:

The code is not too much. It mainly loads pictures from the cache first. When the loaded pictures are empty, then load pictures from the picture address of the mobile phone

bitmap = revitionImageSize(imageView,sourcePath);

I won't say much about loading cache images. I can see it clearly. Mlrucache get(key); It's that simple

Specifically analyze the revisionimagesize() method:

I also wrote comments on the code. Generally, when loading pictures, there is a certain compression of the pictures to avoid oom, so the above processing method is also very common. The pictures to be displayed are compressed according to the size of the ImageView control.

If you don't understand the image compression, let me explain it briefly:

After loading the picture first:

BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path)));

then:

options. inJustDecodeBounds = true;

Then:

At this time, note that the program does not really load the picture, options inJustDecodeBounds = true;

This sentence works, but we get the information about the width and height of the picture, so we can process the compressed picture!

After compressing the picture:

options. inJustDecodeBounds = false;

Get the compressed picture again:

bitmap = BitmapFactory. decodeStream(in,options);

That's it.

Students who look at the code carefully will find that there is a callback parameter in the displaybmp () method:

The callback interface is as follows:

The specific implementation is to call back at the place where the picture is displayed:

The code comments are also written. Students who don't quite understand can communicate by private letter. In addition, the source code on my GitHub GitHub connection is attached, which can be downloaded for easy operation and understanding:

For your convenience, add the following dependencies to your project:

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
分享
二维码
< <上一篇
下一篇>>