【Android】异步加载图片
本文最后更新于 440 天前,其中的信息可能已经有所发展或是发生改变。

加载方式

异步加载最常用的两种方式:
1.多线程/线程池
2.AsyncTask

1.采用子线程的方式进行加载

1.创建一个类异步获取图片。

MyApplication/app/src/main/java/com/example/myapplication/ImageLoader.java

package com.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class ImageLoader {

    private ImageView mImageView;
    private String murl;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //接受message
            if(mImageView.getTag().equals(murl)) {
                //url相同的时候才设置view,防止图片加载的是一样的
                mImageView.setImageBitmap((Bitmap) msg.obj);
            }
        }
    };

    public void showImagerByThread(ImageView imageView, final String url){
        //把两个值保存给上面的子线程使用
        mImageView = imageView;
        murl = url;
        new Thread(){
            @Override
            public void run() {
                super.run();
                Bitmap bitmap = getBitmapFromURL(url);
                Message message = Message.obtain();
                message.obj = bitmap;
                //发送message
                handler.sendMessage(message);
            }
        }.start();
    }

    public Bitmap getBitmapFromURL(String urlString){
        Bitmap bitmap;
        InputStream is = null;
        try {
            URL url = new URL(urlString);
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            is = new BufferedInputStream(connection.getInputStream());
            bitmap = BitmapFactory.decodeStream(is);
            connection.disconnect();
            return bitmap;
        } catch (java.io.IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

2.从view中进行调用

MyApplication/app/src/main/java/com/example/myapplication/NewsAdapter.java

String url = mList.get(position).getPicUrl();
viewHolder.newPic.setTag(url);
new ImageLoader().showImagerByThread(viewHolder.newPic, mList.get(position).getPicUrl());

2.AsyncTask加载图片

1.编写AsyncTask

在ImageLoader类的基础上继续编写。
MyApplication/app/src/main/java/com/example/myapplication/ImageLoader.java

public void showImageByAsyncTask(ImageView imageView,String url){
    new NewsAsyncTask(imageView, url).execute(url);
}

/**
 * AsyncTask的方式把图片放入view
 */
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap>{

    private ImageView mImageView;
    private String murl;

    public NewsAsyncTask(ImageView imageView, String url){
        mImageView = imageView;
        murl = url;
    }

    @Override
    protected Bitmap doInBackground(String... strings) {
        return getBitmapFromURL(strings[0]);
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        //防止图片重复判断唯一性
        if(mImageView.getTag().equals(murl)) {
            mImageView.setImageBitmap(bitmap);
        }
    }
}

2.从view中进行调用

MyApplication/app/src/main/java/com/example/myapplication/NewsAdapter.java

String url = mList.get(position).getPicUrl();
viewHolder.newPic.setTag(url);
new ImageLoader().showImageByAsyncTask(viewHolder.newPic, mList.get(position).getPicUrl());
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇