自己寫一個(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)系作者
  • 序言:七十年代末阁最,一起剝皮案震驚了整個(gè)濱河市戒祠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌速种,老刑警劉巖姜盈,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異配阵,居然都是意外死亡馏颂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門闸餐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饱亮,“玉大人,你說我怎么就攤上這事舍沙〗希” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵拂铡,是天一觀的道長(zhǎng)壹无。 經(jīng)常有香客問我葱绒,道長(zhǎng),這世上最難降的妖魔是什么斗锭? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任地淀,我火速辦了婚禮,結(jié)果婚禮上岖是,老公的妹妹穿的比我還像新娘帮毁。我一直安慰自己,他們只是感情好豺撑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布烈疚。 她就那樣靜靜地躺著,像睡著了一般聪轿。 火紅的嫁衣襯著肌膚如雪爷肝。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天陆错,我揣著相機(jī)與錄音灯抛,去河邊找鬼。 笑死音瓷,一個(gè)胖子當(dāng)著我的面吹牛对嚼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播绳慎,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼猪半,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了偷线?” 一聲冷哼從身側(cè)響起磨确,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎声邦,沒想到半個(gè)月后乏奥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡亥曹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年邓了,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片媳瞪。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡骗炉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蛇受,到底是詐尸還是另有隱情句葵,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站乍丈,受9級(jí)特大地震影響剂碴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜轻专,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一忆矛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧请垛,春花似錦催训、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至镜雨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間儿捧,已是汗流浹背荚坞。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留菲盾,地道東北人颓影。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像懒鉴,于是被迫代替她去往敵國(guó)和親诡挂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容

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