安卓简易版图片异步加载

配合上一篇的viewpager,加上简易版图片异步加载

public class DownloadImage implements Runnable {
    private final int BUFFER_SIZE = 512;

    private Activity context;
    private String filePath;
    private String ImageName;
    private ImageView image;
    private String url;
    private boolean isDownload;

    /**
     * @param context
     * @param filePath   图片路径
     * @param ImageName  图片名字
     * @param url        图片地址
     * @param image      控件
     * @param isDownload 是否需要下载
     */
    public DownloadImage(Activity context, String filePath, String ImageName, String url, ImageView image, boolean
            isDownload) {
        this.context = context;
        this.filePath = filePath;
        this.url = url;
        this.ImageName = ImageName;
        this.image = image;
        this.isDownload = isDownload;
    }

    @Override
    public void run() {
        try {
            if (isDownload) {
                saveToFile();
            }
            while (FileUtil.exist(filePath + ImageName + ".data")) {
                context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setImageBitmap(getLoacalBitmap(filePath + ImageName + ".data"));
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void saveToFile() throws Exception {
        if (!FileUtil.exist(filePath + ImageName + ".data")) {
            File fileTemp = new File(filePath, ImageName + ".temp");
            if (fileTemp.exists())
                fileTemp.delete();
            if (FileUtil.createFile(filePath, ImageName + ".temp")) {
                FileOutputStream fos;
                BufferedInputStream bis;
                byte[] buf = new byte[BUFFER_SIZE];
                int size;
                bis = new BufferedInputStream(HttpClientTools.download(url));
                fos = new FileOutputStream(fileTemp);
                while ((size = bis.read(buf)) != -1) {
                    fos.write(buf, 0, size);
                }
                FileUtil.renameTo(filePath + ImageName + ".temp", filePath + ImageName + "" +
                        ".data");
                fos.flush();
                fos.close();
                bis.close();
            }
        }
    }

    public static Bitmap getLoacalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis); // /把流转化为Bitmap图片

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}

isDownload是用来下载viewpager中多创建的两张图片,为了使滑动效果看起来更真实

简易图,第三张回到第一张的,向右滑动,显示的是第五个page,松手跳到第二个page,这个isDownload就是处理头尾两张,没必要重复下载图片。

if (FileUtil.exist(filePath + "/" + bean.getImageName() + ".data")) {
                image.setImageBitmap(DownloadImage.getLoacalBitmap(filePath + bean.getImageName() + ".data"));
                image.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        callBack.click(bean);
                    }
                });
            } else {
                image.setBackgroundResource(bean.getDefaultId());
                if (bean.getUrl() != null) {
                    image.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            callBack.click(bean);
                        }
                    });
                    new Thread(new DownloadImage(context, filePath, bean.getImageName(), bean.getUrl(), image,
                            isDownload)).start();
                }
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值