自己写一个图片缩略图加载逻辑

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.

*

*管理文件上所有图标

*/

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);

}

});

}

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本人初学Android,最近做了一个实现安卓简单音乐播放功能的播放器,收获不少,于是便记录下来自己的思路与知识总结...
    落日柳风阅读 19,233评论 2 41
  • 想到视频录制,肯定又是运用MediaRecorder,这个类实在是方便,但是好用的东西一定要慎重,毕竟没有那么便宜...
    2c2z0阅读 2,133评论 0 2
  • jar compile'com.android.support:recyclerview-v7:26+' comp...
    吃货养成记阅读 438评论 0 1
  • 知行合一一直是我所追求的最高目标,比起想,去做好就困难的多。 对于家庭 财务方面,我要构建好家庭的财务体系,从储蓄...
    A味良明查阅读 1,323评论 1 50
  • 回来的路上,有一种莫名的感动涌动过心头——你多久没有叫着我的名字,跟我打过招呼了,今天,在超市,很是意外。 我也一...
    我是Molly木有花阅读 559评论 4 6