[size=medium] 有时候图片很长,预览只是其一部分,有个类似按钮点击之后加载全图并显示,类似于GIF的加载显示。
ListAdapter中:[/size]
else if("lmg".equals(data.type)){
Drawable drawable=lmgload.loadImage(viewparent,tagpre,data.dataurl,null);
if(drawable!=null){
LayoutParams lp=holder.rl_data.getLayoutParams();
lp.height=itemwidth*drawable.getMinimumHeight()/drawable.getMinimumWidth();
holder.rl_data.setLayoutParams(lp);
if(holder.tv_lmgctrl!=null){
holder.tv_lmgctrl. setVisibility(View.GONE);
holder.rl_data. removeView(holder.tv_lmgctrl);
}
holder.rl_data.setBackground(drawable);
holder.rl_data.setVisibility(View.VISIBLE);
}
else{
LayoutParams lp=holder.rl_data.getLayoutParams();
lp.height=itemwidth*data.height/data.width;
holder.rl_data.setLayoutParams(lp);
holder.rl_data.setTag(tagpre);
Drawable pre=imgload.loadImage(viewparent,tagpre,data.preurl,ilcallback);
holder.rl_data.setBackground(pre);
if(holder.tv_lmgctrl!=null){
holder.rl_data.removeView(holder.tv_lmgctrl);
}
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
holder.tv_lmgctrl=new TextView(activity);
holder.tv_lmgctrl.setText("点击查看全图");
holder.tv_lmgctrl.setGravity(Gravity.CENTER_HORIZONTAL);
holder.tv_lmgctrl.setTextColor(activity.getResources().getColor(R.color.white_dark));
holder.tv_lmgctrl.setTextSize(TypedValue.COMPLEX_UNIT_PX,activity.getResources().getDimension(R.dimen.text_level1));
holder.tv_lmgctrl.setBackgroundResource(R.color.black_normal_88);
int padding=(int)activity.getResources().getDimension(R.dimen.padding_n);
holder.tv_lmgctrl.setPadding(padding,padding,padding,padding);
lp1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
holder.tv_lmgctrl.setLayoutParams(lp1);
holder.tv_lmgctrl.setTag(tagctrllmg);
holder.rl_data.addView(holder.tv_lmgctrl);
OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setOnClickListener(null);
((TextView)v.findViewWithTag(tagctrllmg)).setText("全图加载中...");
lmgload.loadImage(viewparent,tagpre,data.dataurl,llcallback);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
holder.pb_load=new ProgressBar(activity,null,android.R.attr.progressBarStyleSmall);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
holder.pb_load.setLayoutParams(lp);
holder.pb_load.setTag(tagloadlmg);
holder.rl_data.addView(holder.pb_load);
}
};
holder.rl_data.setOnClickListener(listener);
holder.rl_data.setVisibility(View.VISIBLE);
}
}
[size=medium] 图片加载完成的回掉函数:[/size]
private LmageLoadCallback llcallback=new LmageLoadCallback() {
@SuppressLint("NewApi")
@Override
public void onSuccess(Drawable drawable, int tag, View viewParent) {
// TODO Auto-generated method stub
if(drawable!=null){
RelativeLayout rl_data=(RelativeLayout)(viewParent.findViewWithTag(tag));
if(rl_data!=null){
LayoutParams lp=rl_data.getLayoutParams();
lp.height=itemwidth*drawable.getMinimumHeight()/drawable.getMinimumWidth();
rl_data.setLayoutParams(lp);
rl_data.setBackground(drawable);
rl_data.findViewWithTag(tag+1).setVisibility(View.GONE);
rl_data.findViewWithTag(tag-1).setVisibility(View.GONE);
}
}
}
};
[size=medium] 长图加载的类与图片加载的类相同:[/size]
public class LmageLoad {
private Context context;
private Map imageMap;
private ThreadPoolExecutor executor = null;
BlockingQueue queue =null;
private String path=Environment.getExternalStorageDirectory()+"/Dp Notes/Cache/LonImage";
private int threadMaxNum=Runtime.getRuntime().availableProcessors()>1?Runtime.getRuntime().availableProcessors():2;
private int cacheMaxNum=1;
@SuppressLint("NewApi")
public LmageLoad(Context context){
this.context=context;
imageMap=new LinkedHashMap();
queue =new ArrayBlockingQueue(this.threadMaxNum);
executor=new ThreadPoolExecutor(this.threadMaxNum,this.threadMaxNum,1,TimeUnit.MINUTES,queue,new ThreadPoolExecutor.CallerRunsPolicy());
// File dir=new File(this.path);
// if(!dir.exists()){
// dir.mkdirs();
// }
}
public void setcacheMaxNum(int maxNum){
this.cacheMaxNum=maxNum;
}
public Drawable loadImage(final View viewParent,final int viewTag,final String imageUrl,final LmageLoadCallback callback){
if (imageMap.containsKey(imageUrl)) {
Drawable drawable=imageMap.get(imageUrl);
return drawable;
}
if(callback==null){
return null;
}
// int index=imageUrl.lastIndexOf("/");
// index=index>=0?index:0;
// String filename=imageUrl.substring(index);
// final String filepath=path+filename+".0";
//
// final File mf=new File(filepath);
// if(mf.exists()){
// Bitmap bitmap=BitmapFactory.decodeFile(filepath);
// BitmapDrawable draw=new BitmapDrawable(context.getResources(),bitmap);
// if(draw!=null){
// imageMap.put(imageUrl,draw);
// chackMapSize();
// return draw;
// }
// }
imageMap.put(imageUrl,null);
final Handler handler = new Handler() {
@SuppressLint("HandlerLeak")
public void handleMessage(Message message) {
if(callback!=null){
if(message.what==1){
callback.onSuccess((Drawable) message.obj,viewTag,viewParent);
}
}
}
};
executor.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
URL url = null;
InputStream inputStream = null;
try {
url = new URL(imageUrl);
inputStream = url.openStream();
Drawable drawable = Drawable.createFromStream(inputStream, "src");
Message message;
message=handler.obtainMessage(1,drawable);
handler.sendMessage(message);
if(drawable!=null){
imageMap.put(imageUrl,drawable);
}
else{
imageMap.remove(imageUrl);
}
chackMapSize();
// if(drawable!=errorDrawable){
// try {
// if(!mf.exists()){
// try {
// mf.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
// FileOutputStream fout=new FileOutputStream(mf);
// Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
return null;
}
private void chackMapSize(){
if(imageMap.size()>cacheMaxNum){
for(Entry m:imageMap.entrySet()){
imageMap.remove(m.getKey());
break;
}
}
}
public interface LmageLoadCallback {
public void onSuccess(Drawable drawable,int tag,View viewParent);
}
}
[size=medium] 容易理解,运行:[/size]
[img]http://dl2.iteye.com/upload/attachment/0125/3716/c02b0d71-838c-3b94-a040-ae94f32d0072.gif[/img]
[color=gray][size=medium]注:这是一个.gif动图,ctrl点击图片查看[/size][/color]
[size=medium]
当GIF图片滑动出屏幕时,暂停,需要之前的QRecyclerListener了:[/size]
public ListAdapter(Activity activity,QListView listview){
…
listview.setQRecyclerListener(rclistener);
}
private QRecyclerListener rclistener=new QRecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
// TODO Auto-generated method stub
Holder holder=(Holder)view.getTag();
if(holder!=null&&holder.rl_data!=null){
if(holder.tv_lmgctrl!=null){
holder.rl_data.removeView(holder.tv_lmgctrl);
holder.tv_lmgctrl=null;
}
if(holder.giv_data!=null){
holder.rl_data.removeView(holder.giv_data);
holder.giv_data=null;
}
if(holder.tv_gifctrl!=null){
holder.rl_data.removeView(holder.tv_gifctrl);
holder.tv_gifctrl=null;
}
if(holder.pb_load!=null){
holder.rl_data.removeView(holder.pb_load);
holder.pb_load=null;
}
}
}
};
[size=medium] 在Item滑出屏幕的监听器中,将动态添加的GifImageView、按钮、进度条移除就可以了。[/size]
[align=right][size=medium]路在脚下——2017/06/10[/size][/align]