Android進階之Bitmap的高效加載

一、Bitmap的加載

BitmapFactory提供了四個方法:

  • docodeFiles
  • decodeResource
  • decodeStream
  • decodeByteArray

二、Bitmap的高效加載

采用Bitmap.Options來加載所需尺寸的圖片,主要使用它的inSampleSize參數(shù),當inSampleSize大于1縮放。(inSampleSize會向下去2的指數(shù))
高效加載圖片的流程:

  1. 將BitmapFactory.Options的inJustDecodeBuunds設(shè)為true并加載圖片
  2. 從Bitmap.Options中取出圖片的寬高(outHeight,outWidth)
  3. 結(jié)合ImageView的寬高計算采樣率
  4. 將BitmapFatory.Options中的inJustDecodeBounds設(shè)為false并加載圖片
    示例:
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final ImageView image=findViewById(R.id.image1);
   image.setImageBitmap(sampleDecodeFromResourse(getResources(),R.mipmap.dog,200,200));
    }
    public static Bitmap sampleDecodeFromResourse(Resources res,int resId,int repWidth,int repHeight){
        final BitmapFactory.Options options=new BitmapFactory.Options();
        options.inJustDecodeBounds=true;
        BitmapFactory.decodeResource(res,resId,options);
        options.inSampleSize=computeInSampleSize(options,repWidth,repHeight);
        options.inJustDecodeBounds=false;
        Bitmap bitmap=BitmapFactory.decodeResource(res,resId,options);
        return bitmap;
    }
    public static int computeInSampleSize(BitmapFactory.Options options,int repWidth, int repHeight){
        final int width=options.outWidth;
        final int height=options.outHeight;
        int inSampleSize=1;
        if(width>repWidth||height>repHeight){
            int sampleWidth=((int)width/repWidth)%2==0?width/repWidth+2:width/repWidth+1;
            int sampleHeight=((int)height/repHeight)%2==0?height/repHeight+2:height/repHeight+1;
            inSampleSize=(int)Math.max(sampleWidth,sampleHeight);
        }
        return inSampleSize;
    }
}

三、Android中的緩存策略

常用的緩存算法LRU算法,包括:LruCache和DiskLruCache
LruCache用于內(nèi)存緩存
DiskLruCache用于存儲設(shè)備緩存

(1)LruCache

LruCache是Android提供的一個緩存類,它是一個泛型類,內(nèi)部采用LinkedHashMap以強引用的方式存儲外界對象。
LruCache典型初始化:

int maxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
int cacheSize=maxMemory/8;
mMemoryCache=new LruCache<String,Bitmap>(cacheSize){
protected int sizeOf(String key,Bitmap bitmap){
return bitmap.getByteCount();
}
}

添加一個緩存對象

mMemoryCache.put(key,value);

獲取一個緩存對象

mMemory.get(key);
(2)DiskLruCache

DiskLruCache用于設(shè)備的存儲,及磁盤緩存。需要添加依賴:

implementation 'com.jakewharton:disklrucache:2.0.2'

DiskLruCache的建立:

Private static final int CACHE_SIZE=1024*1024*50;
File diskCacheDir=getDiskCacheDir(mContext,"bitmap");
if(!diskCacheDir.exists())

首先獲取Url所對應(yīng)的Key,再根據(jù)key通過edit()方法獲取Editor對象,一般key取url的MD5值

private String hashKeyFormUrl(String url){
String hashKey;
try{
final MessageDigest mDigest=MessageDigest.getInstance("MD5");
mDigest.upDtate(url.getByte());
hashKey=bytesToHexString(mDigest.digest());
}catch(NoSuchAlgorithmException e){
hashKey=String.valueOf(url.hashCode());
}
return hashKey;
}
private String bytesToHexString(byte[] bytes){
StringBuffer buffer=new StringBuffer();
for(int i=0;i<bytes.length;i++){
String hex=Integer.toHexString(0xFF&bytes[i]);
if(hex.length==1){
buffer.append('0');
}
buffer.append(hex);
}
return buffer;
}

獲取Editor對象

String key=hashFormUrl(url);
DiskLruCache.Editor editor=mDiskLruCache.edit(key);
if(editor!=null){
OutputStream outputStream=editor.newOutputStream(DISK_CACHE_INDEX);
}

存入緩存對象

public boolean downloadUrlToStream(String url,OutputStream outputStream){
HttpURLConnection connection=null;
BufferedOutputStream out=null;
BufferedInputStream int=null;
try{
final URL url=new URL(url);
connection=(HttpURLConnection)url.openConnection();
in=new BufferedInputStream(connection.getInputStream(),1024*10);
out=new BufferedOutputStream(output,1024*10);
int b;
while((b=in.read())!=-1){
out.write(b);
}
return true;
}catch(IOException e){
e.printStackTrace();
}finally{
if(connection!=null){
connection.disConnection();
}
if(in!=null){
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(out!=null){
try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}

提交

OutputStream outputStream=editor.newOutputStream(DISK_CACHE_INDEX);
if(downloadUrlToStream(url,outputStream)){
editor.commit();
}else{
editor.abort();
}
mDiskLruCache.flush();

提取

Bitmap bitmp=null;
String key=hashKeyFormUrl(url);
DiskLruCache.Snapshot snapshot=mDiskLruCache.get(key);
if(snapshot!=null){
FileInputStream fileInputStream=(FileInputStream)snapshot.getInputStream(DISK_CACHE_INDEX);
FileDescriptor fileDescriptor=fileInputStream.getFD();//由于使用流解析存在問題,所以采取流對應(yīng)的文件描述符來解析
bitmap=decodeSampleedBitmapFromFileDescriptor(fileDescriptor,repWidth,repHeight);//自己寫的高效加載圖片的方法和前面的一樣只是解析方法不一樣
if(bitmap!=null){
addBitmapTomemoryCache(key,bitmap);
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容