1. 程式人生 > >Android的listview中圖片載入的優化

Android的listview中圖片載入的優化

費了好大勁寫玩才發現早就有現成的三方(glide)了,還是記下來吧。

為什麼優化?因為圖片大了很容易超出記憶體導致崩潰。

原理很簡單,壓縮圖片,儲存。

工具類。

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/4/1.
 */

public class SaveThumbUtils {
    //這個thumbpath是工作目錄,
    public static void saveThumb(String imagePath,String workPath){
        int width=100;//縮圖的寬
        int height=100;//縮圖的高
        Bitmap bitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 獲取這個圖片的寬和高,注意此處的bitmap為null
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; // 設為 false
        // 計算縮放比
        int h = options.outHeight;
        int w = options.outWidth;
        int beWidth = w / width;
        int beHeight = h / height;
        int be = 1;
        if (beWidth < beHeight) {
            be = beWidth;
        } else {
            be = beHeight;
        }
        if (be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        // 重新讀入圖片,讀取縮放後的bitmap,注意這次要把options.inJustDecodeBounds 設為 false
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        // 利用ThumbnailUtils來建立縮圖,這裡要指定要縮放哪個Bitmap物件
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        File f = new File(workPath+"/thumb/"+imagePath.replace("/","").replace(".",""));
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
然後oncreate載入時。
 try {
                        final ImageView oldImage = imgDrawShow;
                        final String tfilepath = filepath;
                        File tFile = new File(sd + "/thumb/" + filepath.replace("/", "").replace(".", ""));
                        if (!tFile.exists()) {
                            new Thread() {
                                public void run() {
                                    SaveThumbUtils.saveThumb(tfilepath, sd);
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            final Bitmap bm = BitmapFactory.decodeFile(sd + "/thumb/" + tfilepath.replace("/", "").replace(".", ""));
                                            oldImage.setImageBitmap(bm);
                                        }
                                    });
                                }
                            }.start();
                        }
                        else {
                            Bitmap bm = BitmapFactory.decodeFile(sd + "/thumb/" + filepath.replace("/", "").replace(".", ""));
                            imgDrawShow.setImageBitmap(bm);
                        }
                    }catch (Exception e){}

新增時。

new Thread() {
                public void run() {
                    SaveThumbUtils.saveThumb(filePath,sd);
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            try {
                                Bitmap bm = BitmapFactory.decodeFile(sd+"/thumb/"+filePath.replace("/","").replace(".",""));
                                imgDrawShow.setImageBitmap(bm);
                            }catch (Exception e){
                            }
                        }
                    });
                }
            }.start();