码迷,mamicode.com
首页 > 其他好文 > 详细

图片压缩与缓存

时间:2015-07-13 15:48:45      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.wangzhen.util;
  2 
  3 import java.io.ByteArrayInputStream;
  4 import java.io.ByteArrayOutputStream;
  5 import java.io.IOException;
  6 import java.net.HttpURLConnection;
  7 import java.net.MalformedURLException;
  8 import java.net.URL;
  9 
 10 import android.annotation.SuppressLint;
 11 import android.graphics.Bitmap;
 12 import android.graphics.BitmapFactory;
 13 import android.graphics.Bitmap.CompressFormat;
 14 import android.os.Handler;
 15 import android.os.Message;
 16 import android.support.v4.util.LruCache;
 17 import android.util.Log;
 18 import android.widget.ImageView;
 19 import android.widget.ImageView.ScaleType;
 20 
 21 /**
 22  * 图片工具类
 23  * 
 24  * @author Administrator
 25  * 
 26  */
 27 public class ImageLoader {
 28 
 29     private static String mURL = "";
 30     private static ImageView mImageView;
 31     private LruCache<String, Bitmap> mCaches;// LruCache缓存算法
 32 
 33     public ImageLoader() {
 34         // 获取最大可用内存
 35         int MaxMemory = (int) Runtime.getRuntime().maxMemory();
 36         // 设置缓存可用空间
 37         int CacheSize = MaxMemory / 4;
 38         mCaches = new LruCache<String, Bitmap>(CacheSize) {
 39             @SuppressLint("NewApi")
 40             @Override
 41             protected int sizeOf(String key, Bitmap value) {
 42                 // 每次存入缓存的时候调用
 43                 return value.getByteCount();
 44             }
 45         };
 46     }
 47 
 48     /**
 49      * 将图片存入缓存
 50      * 
 51      * @param url
 52      * @param bitmap
 53      */
 54     private void AddBitmapToCache(String url, Bitmap bitmap) {
 55         if (GetBitmapFromCache(url) == null) {
 56             mCaches.put(url, bitmap);
 57         }
 58     }
 59 
 60     /**
 61      * 从缓存中取出图片
 62      * 
 63      * @param url
 64      * @return
 65      */
 66     private Bitmap GetBitmapFromCache(String url) {
 67         return mCaches.get(url);
 68     }
 69 
 70     /**
 71      * 以线程方式下载图片
 72      * 
 73      * @param imageView
 74      * @param path
 75      */
 76     public void GetImageByThread(ImageView imageView, String path) {
 77         mURL = path;
 78         mImageView = imageView;
 79         new Thread() {
 80             public void run() {
 81                 // 首先判断缓存中是否已存在当前图片
 82                 Bitmap bitmap = GetBitmapFromCache(mURL);
 83                 if (bitmap == null) {
 84                     bitmap = CompressBitmap(GetBitmapFromURL(mURL));
 85                     Message message = Message.obtain();
 86                     message.obj = bitmap;
 87                     mHandler.sendMessage(message);
 88                 } else {
 89                     mImageView.setImageBitmap(bitmap);
 90                 }
 91             };
 92         }.start();
 93     }
 94 
 95     /**
 96      * 通过URL获取图片
 97      * 
 98      * @return
 99      */
100     private Bitmap GetBitmapFromURL(String path) {
101         Bitmap bitmap = null;
102         try {
103             URL url = new URL(path);
104             HttpURLConnection connection = (HttpURLConnection) url
105                     .openConnection();
106             bitmap = BitmapFactory.decodeStream(connection.getInputStream());
107             connection.disconnect();
108         } catch (MalformedURLException e) {
109             // TODO Auto-generated catch block
110             e.printStackTrace();
111         } catch (IOException e) {
112             // TODO Auto-generated catch block
113             e.printStackTrace();
114         }
115         return bitmap;
116     }
117 
118     /**
119      * 图片压缩:质量压缩
120      * 
121      * @param bitmap
122      * @return
123      */
124     private Bitmap CompressBitmap(Bitmap bitmap) {
125         ByteArrayOutputStream baos = new ByteArrayOutputStream();
126         // 质量压缩方法,这里100表示不压缩,将压缩后的数据放入到baos中
127         bitmap.compress(CompressFormat.JPEG, 100, baos);
128         Log.i("Compress", "压缩前:" + baos.toByteArray().length / 1024 + "KB");
129         int options = 100;
130         while (baos.toByteArray().length / 1024 > 50) {
131             // 重置BAOS,即清空
132             baos.reset();
133             bitmap.compress(CompressFormat.JPEG, options, baos);
134             options -= 10;
135         }
136         Log.i("Compress", "压缩后:" + baos.toByteArray().length / 1024 + "KB");
137         // 将压缩后数据存放到ByteArrayInputStream中
138         ByteArrayInputStream is = new ByteArrayInputStream(baos.toByteArray());
139         // 把ByteArrayInputStream解码为图片
140         Bitmap image = BitmapFactory.decodeStream(is);
141         return image;
142     }
143 
144     Handler mHandler = new Handler() {
145         @SuppressLint("NewApi")
146         public void handleMessage(Message msg) {
147             Bitmap bitmap =(Bitmap) msg.obj;
148             AddBitmapToCache(mURL, bitmap);
149             mImageView.setImageBitmap(bitmap);
150             //按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽) 
151             mImageView.setScaleType(ScaleType.CENTER_CROP);
152         };
153     };
154 
155 }

 

图片压缩与缓存

标签:

原文地址:http://www.cnblogs.com/lavalike/p/4642655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!