实现简单的毕加索
来源:互联网 发布:windows gui编程matlab 编辑:程序博客网 时间:2024/04/27 18:44
/** * Created by Administrator on 2016/10/8. * 它有4个方法 * 1.获取数据 * 》先从内存中获取 * 》没有再从硬盘上获取 * 2.加载数据 * 从网络上下载 * 3.保持数据 * 》先保持到硬盘上 * 》再保持到内存中 * 4.删除数据 */public class MyCache { static final String path = Environment.getExternalStorageDirectory() + File.separator + "/cache"; int type; //内部存储 LruCache<String, Bitmap> innerCache; //外部存储 File dir; //这里是压缩是传入的源图片 ImageView imageView; //是否对图片资料压缩 boolean isFlag; public MyCache() { this(10, 1); } public MyCache(int size, int type) { this.type = type; innerCache = new LruCache<>(size); dir = new File(path); if (!dir.exists()) dir.mkdir(); } public void setImageView(ImageView imageView) { this.imageView = imageView; } public void setFlag(boolean isFlag) { this.isFlag = isFlag; } //获取数据 public Bitmap get(String key) { //把传入的字符串做一定处理如www://hao123.com变为www___hao123_com if (key == null) return null; key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_"); Bitmap value = null; //从内存中获取 value = innerCache.get(key); if (value != null) return value; File file = new File(dir, key + ".jpeg"); if (!file.exists()) return value; //从外部文件获取 byte[] datas = getFromFile(file); return compressBitmap(datas); } //从网上加载并显示出来 public Bitmap loadDataFromNet(String key) { byte[] data = getDataFromNet(key); if (data == null) return null; //保持到内存中 Bitmap bitmap = compressBitmap(data); saveInner(key, bitmap); //保持到硬盘上 saveOutter(key, bitmap); return bitmap; } private void saveInner(String key, Bitmap bitmap) { key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_"); innerCache.put(key, bitmap); } private void saveOutter(String key, Bitmap bitmap) { key = key.replace(".", "_").replace("//", "_").replace(":", "_").replace("/","_"); FileOutputStream fos = null; try { fos = new FileOutputStream(new File(dir, key + ".jpeg")); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } //从外部文件上获取图片的字节对象 private byte[] getFromFile(File file) { BufferedInputStream bis = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bis = new BufferedInputStream(new FileInputStream(file)); byte[] bufs = new byte[2048]; int len = -1; while ((len = bis.read(bufs)) != -1) { bos.write(bufs, 0, len); } bos.flush(); return bos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { if (bis == null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } //对图片的压缩与否 public Bitmap compressBitmap(byte[] datas) { if (imageView == null && !isFlag) return BitmapFactory.decodeByteArray(datas, 0, datas.length); BitmapFactory.Options options = new BitmapFactory.Options(); if (imageView != null) { options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(datas, 0, datas.length, options); int width = options.outWidth; int height = options.outHeight; imageView.measure(0, 0); int imagWidth = imageView.getMeasuredWidth(); int imagHeight = imageView.getMeasuredHeight(); int temp1 = width / imagWidth; int temp2 = height / imagHeight; temp1 = temp1 > temp2 ? temp1 : temp2; options.inSampleSize = temp1 > 1 ? temp1 : 1; } if (isFlag) options.inPreferredConfig = Bitmap.Config.RGB_565; options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(datas, 0, datas.length, options); } //删去指定文件夹下所有的文件 public void deleteAll() { if (!dir.exists()) return; deleteData(dir); } private void deleteData(File root) { File[] files = root.listFiles(); if (files != null) { for (File file : files) { deleteData(file); } } root.delete(); } private byte[] getDataFromNet(String path) { BufferedInputStream bis = null; try { URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { bis = new BufferedInputStream(connection.getInputStream()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] bufs = new byte[2048]; int len = -1; while ((len = bis.read(bufs)) != -1) { bos.write(bufs, 0, len); } bos.flush(); return bos.toByteArray(); } } catch (Exception e) { e.printStackTrace(); } finally { if (bis == null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }}/** * Created by Administrator on 2016/10/8. */public class MyPicaso { private static ExecutorService threadPool = Executors.newFixedThreadPool(30); private static Context context; private static MyPicaso instance; private String path; private int id; private static MyCache cache; boolean compress; private MyPicaso() { } //单列模式 public static MyPicaso with(Context temp) { if (instance != null) return instance; instance = new MyPicaso(); //防止内存泄漏 context = temp.getApplicationContext(); cache = new MyCache(); return instance; } //加载地址 public MyPicaso load(String path) { this.path = path; return this; } //设置站位图片 public MyPicaso placeHolder(int id) { this.id = id; return this; } //是否压缩 public MyPicaso setCompress(boolean compress) { this.compress = compress; return this; } //把获得图片设置上去 public void into(final ImageView imageView) { Bitmap bitmap = cache.get(path); if (bitmap != null) { imageView.setImageBitmap(bitmap); return; } if (compress) { cache.setImageView(imageView); cache.setFlag(true); } final String temp = path; imageView.setTag(temp);//设置标记 imageView.setImageResource(id); threadPool.execute(new Runnable() { @Override public void run() { final Bitmap bitmap1 = cache.loadDataFromNet(temp); //这里的判断主要是为了防止复用造成的图片错位 if (imageView.getTag() != null && imageView.getTag().equals(temp)) { imageView.post(new Runnable() { @Override public void run() { imageView.setImageBitmap(bitmap1); } }); } } }); } public void deleteCache(){ cache.deleteAll(); }}
0 0
- 实现简单的毕加索
- 毕加索实现APP开起的Wellcome图片
- 贴吧涂鸦–毕加索的画板
- 毕加索传记的艺术和历史
- Picosso毕加索网络加载图片的框架
- 毕加索的艺术——Picasso,一个强大的Android图片下载缓存库,OkHttpUtils的使用,二次封装PicassoUtils实现微信精选
- 自比毕加索,揭秘神秘的“苹果大学”
- 【BIM+毕加索】BIM信息化的时代已经到来
- 盗版毕加索
- 寻找明星级别的高级工程师无异于让毕加索去给你的房子刷油漆
- 人工智能学习梵高,毕加索风格,画出的世界名画是什么样子
- 毕加索携手北京桑博士幼教园打造省心,安心,放心的智能幼儿园
- 毕加索加载圆形图片
- 学习Picasso(毕加索)
- Android Picasso(毕加索)
- 蒋勋看毕加索-有声版
- 杀毒软件的简单实现的简单实现
- Picasso(毕加索)框架 学习笔记
- SEH 0XC0000005
- "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- Target runtime com.genuitec.runtime.generic.jee60 is not defined.
- eclipse生成javadoc
- git config找到下载的路径URL
- 实现简单的毕加索
- codevs1219 骑士游历 棋盘型DP
- 用RadioGroup设置选择题
- AndroidManifest.xml配置文件详解
- JS事件——禁止事件冒泡和禁止默认事件
- 深入浅出JSONP--解决ajax跨域问题
- git 回滚到指定的版本
- android模拟器访问本地网站
- c++内联