安卓简易版图片异步加载
来源:互联网 发布:java过滤器配置 编辑:程序博客网 时间:2024/04/27 16:38
配合上一篇的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
- 安卓简易版图片异步加载
- jQuery版图片特效代码
- 【安卓笔记】异步加载大量图片
- 安卓处理Html----图片异步加载
- 安卓LoaderManager异步加载数据库
- 安卓GridView异步加载网络图片
- 【安卓笔记】异步加载大量图片
- 安卓AsyncTask异步加载图片
- 安卓入门--Loaders异步加载数据
- 安卓之异步加载AsyncTask
- 仿京东简单版图片放大
- FastDFS单机版图片服务器
- Android简易实战教程--第二十八话《加载大图片》
- 安卓图片异步加载开源库:Universal Image Loader
- 安卓代码优化 使用ImageLoader实现图片异步加载
- 安卓开发中listview异步加载图片
- 安卓ListView图片异步加载错位的处理
- 安卓universal-image-loader图片异步加载的解析
- iOS程序逆向Mac下常用工具——Reveal、HopperDisassemble、IDA
- [LeetCode]--143. Reorder List(Python + Java)
- 嘟嘟聊Unity:写给不知如何学Unity3D的爱好者
- linq ,lamda 进行分组GroupBy查询
- Single Number
- 安卓简易版图片异步加载
- 基于Spring+SpringMVC+Mybatis的秒杀系统之Dao层(1)
- 多重部分和问题
- PS快速蒙版 ——制作动感效果——飞猫
- 网络流(最大流+模板)
- 教你上传本地代码到github
- Android ANR产生的原因及如何避免
- A*算法
- 又见导弹拦截