The principle of asynchronous loading of ImageLoader 1--solving the problem of stuck

            I encountered a problem when I was making an audio and video player more than a month ago, that is to display the first frame of each video in the video list, the first frame is displayed, but there is a problem when sliding, it is seriously stuck !! ! At first, I thought about compressing the picture, and it would still be stuck after compression. Then I thought of opening a sub-thread (because the main thread cannot perform time-consuming operations in Android), and put the operation of getting the first frame of the video into it. At that time, the old problems were not solved, new problems appeared, and the pictures and videos were misplaced. After checking a lot of information, they still did not solve them well. Until today, this problem was finally solved perfectly. Also wrote the ImageLoader class, about 50 lines of code. We put the operation of getting the first frame of the video into the child thread, according to the incoming image (the picture you want to display at that position, that is, obtained by findViewById in the getView method in the Adapter class), url (the path of the video) )

   

package zkx.com.mobileplayer.util;

/**
 * Created by zhang on 2016/10/14.
 */

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;


/**
 * Created by zhang on 2016/11/27.
 */
public class ImageLoader {

    private ImageView mImageView;
    private String mUrl;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mImageView.getTag().equals(mUrl)) {
                mImageView.setImageBitmap((Bitmap) msg.obj);
            }

        }
    };


    /**
     * @param imageView
     * @param url
     */
    public void showImageByThread(ImageView imageView, final String url) {
        mImageView = imageView;
        mUrl = url;
        new Thread() {
            @Override
            public void run() {
                super.run();
                MediaMetadataRetriever media = new MediaMetadataRetriever ();
                media.setDataSource(url);
                Bitmap bitmap = media.getFrameAtTime(3000);
                bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 100);
                Message message = Message.obtain();
                message.obj = bitmap;
                mHandler.sendMessage(message);
            }
        }.start();

    }

}

 

 

    Then in the getView method of your Adapter class, add these sentences, note: here we set a unique tag tag for iv_icon_video (through the code holder.iv_icon_video.setTag(url), the url is the unique tag tag) , which is equivalent to setting a unique identity information for iv_icon_video. The purpose is to avoid image display misalignment due to the caching mechanism of ListView , and then call the showImageByThread method of ImageLoader and pass in holder.iv_icon_video and url (the path of the video). Then the first frame of the video is obtained through the showImageByThread method. We send the obtained bitmap to the main thread through the Hander, which solves the problem that the sub-line cannot update the UI, and then processes the bitmap in the handlerMessage method, then we in the Adapter The set flag tag is about to play a role. We judge whether the tag is the same, if it is the same, it will be set, and if it is different, it will not be processed, then the problem of misalignment will be solved.

 

 

        //Set the default image to display
        holder.iv_icon_video.setImageResource(R.drawable.video_default_icon);
        String url = videoItem.getPath();
        holder.iv_icon_video.setTag(url);
        new ImageLoader().showImageByThread(holder.iv_icon_video, url);

 

 Another point to say is that the two lines of code in showImageByThread, their function is the same as the ViewHolder mode cache view in BaseAdapter, they also cache imageView and url

 

       mImageView = imageView;
       mUrl = url;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326425401&siteId=291194637
Recommended