自己寫一個(gè)圖片縮略圖加載邏輯

packagexcxin.filexpert.view.adpter.dataAdapter;

importandroid.graphics.Bitmap;

importandroid.graphics.drawable.BitmapDrawable;

importandroid.graphics.drawable.Drawable;

importandroid.support.annotation.NonNull;

importandroid.view.View;

importandroid.widget.ImageView;

importjava.util.concurrent.ExecutorService;

importjava.util.concurrent.PriorityBlockingQueue;

importjava.util.concurrent.ThreadPoolExecutor;

importjava.util.concurrent.TimeUnit;

importrx.Observable;

importrx.android.schedulers.AndroidSchedulers;

importrx.functions.Action1;

importrx.functions.Func1;

importxcxin.filexpert.R;

importxcxin.filexpert.assistant.cache.ThumbCache;

importxcxin.filexpert.assistant.utils.AppThumbUtils;

importxcxin.filexpert.assistant.utils.TimeUtils;

importxcxin.filexpert.model.implement.FileInfo;

importxcxin.filexpert.orm.dao.LabelFile;

importxcxin.filexpert.orm.helper.DbUtils;

importxcxin.filexpert.orm.helper.implement.LabelFileHelper;

/**

* Created by liucheng on 2015/11/2.

*

*管理文件上所有圖標(biāo)

*/

public classThumbWorkManger {

private staticThumbWorkMangerinstance;

privateExecutorServicemThumbPool=null;

public staticThumbWorkMangergetInstance() {

if(instance==null) {

instance=newThumbWorkManger();

}

returninstance;

}

privateThumbWorkManger() {

this.mThumbPool=newThreadPoolExecutor(5,Integer.MAX_VALUE,

60L,TimeUnit.SECONDS,

newPriorityBlockingQueue());

}

private static classBaseThumbThreadimplementsComparable {

private longmTimeFlag;

publicBaseThumbThread() {

this.mTimeFlag= TimeUtils.getCurrentTime();

}

@Override

public intcompareTo(@NonNullBaseThumbThread another) {

return(int) another.getTimeFlag() - (int)mTimeFlag;

}

public longgetTimeFlag() {

returnmTimeFlag;

}

}

public voidgetLocalThumb(intid,BaseViewHolder holder,

FileInfo fileInfo, booleanisList) {

mThumbPool.execute(newLocalThumbThread(id,holder,fileInfo,isList));

}

private classLocalThumbThreadextendsBaseThumbThreadimplementsRunnable {

private intmId;

privateBaseViewHoldermHolder;

privateFileInfomFileInfo;

private booleanmIsList;

publicLocalThumbThread(intid,BaseViewHolder holder,

FileInfo fileInfo, booleanisList) {

super();

this.mId= id;

this.mHolder= holder;

this.mFileInfo= fileInfo;

this.mIsList= isList;

}

@Override

public voidrun() {

setLocalThumb(mFileInfo,mIsList,mHolder,mId);

}

}

private voidsetLocalThumb(FileInfo fileInfo, final booleanisList,

finalBaseViewHolder holder, intid) {

Bitmap bitmap = fileInfo.getThumb(isList);

finalString thumbKey = String.valueOf(isList) + id + fileInfo.getPath();

if(bitmap !=null) {

ThumbCache.getCache().putThumb(thumbKey,bitmap,isList);

Observable.just(bitmap)

.filter(newFunc1() {

@Override

publicBooleancall(Bitmap bitmap) {

returnthumbKey.equals(holder.getThumbView().getTag());

}

})

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newAction1() {

@Override

public voidcall(Bitmap bitmap) {

setThumbImpl(holder,isList,bitmap);

}

});

}

}

private voidsetThumbImpl(BaseViewHolder holder, booleanisList,Bitmap cacheBitmap) {

try{

if(null!= cacheBitmap) {

holder.getThumbCircleLayout().setVisibility(View.GONE);

if(isList) {

holder.getThumbView().setImageBitmap(cacheBitmap);

}else{

holder.getThumbView().setVisibility(View.GONE);

holder.getBigThumbView().setVisibility(View.VISIBLE);

holder.getBigThumbView().setImageBitmap(cacheBitmap);

}

}

}catch(Exception e) {

e.printStackTrace();

}

}

public voidgetAppIcon(String name,ImageView appIcon) {

mThumbPool.execute(newIconThread(name,appIcon));

}

private classIconThreadextendsBaseThumbThreadimplementsRunnable {

privateStringmName;

privateImageViewmAppIcon;

publicIconThread(String name,ImageView appIcon) {

super();

this.mName= name;

this.mAppIcon= appIcon;

}

@Override

public voidrun() {

Observable.just(mAppIcon)

.map(newFunc1() {

@Override

publicBitmapcall(ImageView imageView) {

if(imageView !=null) {

Drawable drawable = AppThumbUtils.getAppDrawable(mName);

if(drawable !=null) {

return((BitmapDrawable) drawable).getBitmap();

}

}

return null;

}

})

.filter(newFunc1() {

@Override

publicBooleancall(Bitmap bitmap) {

returnbitmap !=null&&

ThumbCache.getCache().putIconToMem(mName,bitmap) &&

mAppIcon.getTag().equals(mName);

}

})

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newAction1() {

@Override

public voidcall(Bitmap bitmap) {

mAppIcon.setImageBitmap(bitmap);

mAppIcon.setVisibility(View.VISIBLE);

}

}, newAction1() {

@Override

public voidcall(Throwable throwable) {

}

});

}

}

public voidgetLabelIcon(String name,ListViewHolder holder) {

mThumbPool.execute(newLabelIconThread(name,holder));

}

private classLabelIconThreadextendsBaseThumbThreadimplementsRunnable {

privateStringmPath;

privateListViewHoldermHolder;

publicLabelIconThread(String path,ListViewHolder holder) {

super();

this.mPath= path;

this.mHolder= holder;

}

@Override

public voidrun() {

finalImageView labelIconView =mHolder.getLabelIcon();

if(labelIconView !=null) {

LabelFileHelper helper = DbUtils.getLabelFileHelper();

if(helper.isLabelFile(mPath)) {

LabelFile labelFile;

try{

labelFile = helper.getLabelFile(mPath);

}catch(Exception e) {

e.printStackTrace();

return;

}

if(labelFile !=null) {

longlabelId = labelFile.getLabelId();

intcolorId;

try{

colorId = DbUtils.getLabelHelper().queryColor(labelId);

}catch(Exception e) {

return;

}

Observable.just(colorId)

.filter(newFunc1() {

@Override

publicBooleancall(Integer integer) {

returnlabelIconView.getTag().equals(mPath);

}

})

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newAction1() {

@Override

public voidcall(Integer colorId) {

if(colorId == -1) {

labelIconView.setImageResource(R.drawable.ic_tag_collection_small);

}else{

labelIconView.setImageResource(colorId);

}

labelIconView.setVisibility(View.VISIBLE);

}

});

}

}

}

}

}

public voidgetNetThumb(intid,BaseViewHolder holder,

FileInfo fileInfo, booleanisList) {

mThumbPool.execute(newNetThumbThread(id,holder,fileInfo,isList));

}

private classNetThumbThreadextendsBaseThumbThreadimplementsRunnable {

private intmId;

privateBaseViewHoldermHolder;

privateFileInfomFileInfo;

private booleanmIsList;

publicNetThumbThread(intid,BaseViewHolder holder,

FileInfo fileInfo, booleanisList) {

this.mId= id;

this.mHolder= holder;

this.mFileInfo= fileInfo;

this.mIsList= isList;

}

@Override

public voidrun() {

finalString thumbKey = String.valueOf(true) +mId+mFileInfo.getPath();

Bitmap bitmap = ThumbCache.getCache().getThumbFromDisk(thumbKey);

if(bitmap ==null) {

bitmap =mFileInfo.getThumb(true);

}

if(bitmap !=null) {

ThumbCache.getCache().putThumbToMemAndDisk(thumbKey,bitmap, true);

Observable.just(bitmap)

.filter(newFunc1() {

@Override

publicBooleancall(Bitmap bitmap) {

returnthumbKey.equals(mHolder.getThumbView().getTag());

}

})

.observeOn(AndroidSchedulers.mainThread())

.subscribe(newAction1() {

@Override

public voidcall(Bitmap bitmap) {

setThumbImpl(mHolder,mIsList,bitmap);

}

});

}

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 本人初學(xué)Android,最近做了一個(gè)實(shí)現(xiàn)安卓簡(jiǎn)單音樂(lè)播放功能的播放器,收獲不少,于是便記錄下來(lái)自己的思路與知識(shí)總結(jié)...
    落日柳風(fēng)閱讀 19,466評(píng)論 2 41
  • 想到視頻錄制,肯定又是運(yùn)用MediaRecorder,這個(gè)類實(shí)在是方便,但是好用的東西一定要慎重,畢竟沒(méi)有那么便宜...
    2c2z0閱讀 2,185評(píng)論 0 2
  • jar compile'com.android.support:recyclerview-v7:26+' comp...
    吃貨養(yǎng)成記閱讀 459評(píng)論 0 1
  • 知行合一一直是我所追求的最高目標(biāo),比起想,去做好就困難的多。 對(duì)于家庭 財(cái)務(wù)方面,我要構(gòu)建好家庭的財(cái)務(wù)體系,從儲(chǔ)蓄...
    A味良明查閱讀 1,532評(píng)論 1 50
  • 回來(lái)的路上,有一種莫名的感動(dòng)涌動(dòng)過(guò)心頭——你多久沒(méi)有叫著我的名字,跟我打過(guò)招呼了,今天,在超市,很是意外。 我也一...
    我是Molly木有花閱讀 713評(píng)論 4 6

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