Imageloader8-压缩图片
生活随笔
收集整理的這篇文章主要介紹了
Imageloader8-压缩图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通過采樣率壓縮圖片的步驟:
BTW: 說一下BitmapFactory.Options的inJustDecodeBounds屬性,當(dāng)參數(shù)設(shè)置為true時(shí),BitmapFactory只會(huì)解析圖片的原始寬和高,并不會(huì)將圖片加載到內(nèi)存中。
// 如果圖片不存在 則添加到任務(wù)隊(duì)列中addTask(new Runnable() {@Overridepublic void run() {// 加載圖片 TODO// 1.獲取圖片需要顯示的寬和搞ImageSize imageSize = getImageViewSize(imageView);// 利用Options壓縮圖片Bitmap bm = decodeSampledBitmapFromPath(path, imageSize.width, imageSize.height);// 添加到LruCache中addBitmapToLruCache(path, bm);// 發(fā)送消息,通知UIHandler更新圖片ImageBeanHoler holder = new ImageBeanHoler();holder.bitmap = getBitmapFromLruCache(path);holder.imageView = imageView;holder.path = path;Message message = Message.obtain();message.obj = holder;mUIHandler.sendMessage(message);// 執(zhí)行完之后,釋放一個(gè)信號(hào)量,通知mPoolThread可以從任務(wù)隊(duì)列中獲取下一個(gè)任務(wù)了去執(zhí)行了。mPoolThreadSemaphore.release();}}); /*** 根據(jù)計(jì)算的inSampleSize得到壓縮的圖片** @param path* @param reqWidth* @param reqHeight* @return*/private Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, int reqHeight) {// 第一次解析將inJustDecodeBounds設(shè)置為true,不將圖片加載到內(nèi)存,獲取圖片的大小BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(path, options);// 計(jì)算inSampleSizeoptions.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);// 使用獲取到的inSampleSize再此獲取圖片,加載到內(nèi)存中options.inJustDecodeBounds = false;Bitmap bitmap = BitmapFactory.decodeFile(path, options);return bitmap;} /*** 這個(gè)方法用戶可以自己設(shè)置適合項(xiàng)目的圖片比例** @param options* @param reqWidth* @param reqHeight* @return*/private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {// 源圖片的寬度int width = options.outWidth;int height = options.outHeight;int inSampleSize = 1;if (width > reqWidth && height > reqHeight) // ||也是可以的,看實(shí)際情況{// 計(jì)算出實(shí)際寬度和目標(biāo)寬度的比率int widthRatio = Math.round((float) width / (float) reqWidth);int heightRatio = Math.round((float) width / (float) reqWidth);inSampleSize = Math.max(widthRatio, heightRatio); // 為了節(jié)省內(nèi)存,取了大值。如果有變形,取小值試試。}return inSampleSize;}
總結(jié)
以上是生活随笔為你收集整理的Imageloader8-压缩图片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Imageloader7-获取图片需要显
- 下一篇: 大数据文件分隔符