使用方法:
/**
* 日记照片加载事件监听
*/
private class OnPicLoadListener extends OnImageLoadListener
{
private int mImageSource = R.drawable.default_pic;
/**
* 设置图片
*
* @param view
* @param imageUrl
* @param tag
* @param drawable
*/
public void setDrawable(ImageView view, String imageUrl, String tag, Drawable drawable)
{
if (view == null) return;
int height = 0;
if (mImagesHeight.containsKey(imageUrl)) {
height = mImagesHeight.get(imageUrl);
}
View ct = ((View) view.getParent()).findViewById(R.id.calendarCt);
if (ct != null) {
ct.setVisibility(View.INVISIBLE);
}
if (drawable != null) {
// 定义图片的最佳高度
if (height == 0) {
int minHeight = ZUI.dp2px(mContext, PIC_MIN_HEIGHT);
int maxHeight = ZUI.dp2px(mContext, PIC_MAX_HEIGHT);
height = (int) ((float) view.getWidth() / drawable.getMinimumWidth() * drawable.getMinimumHeight());
if (height > maxHeight) {
height = maxHeight;
} else if (height < minHeight) {
height = minHeight;
}
mImagesHeight.put(imageUrl, height);
}
// 现将图片完全透明
drawable.setAlpha(0);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, height));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setImageDrawable(drawable);
// 添加透明渐变动画显示图片
AlphaAnimation alphaAnim = new AlphaAnimation(0.0f, 1.0f);
alphaAnim.setDuration(100);
view.setAnimation(alphaAnim);
if (ct != null) {
ct.setVisibility(View.VISIBLE);
}
} else {
int minHeight = ZUI.dp2px(mContext, PIC_MIN_HEIGHT);
height = height < minHeight minHeight : height;
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, height));
view.setScaleType(ImageView.ScaleType.CENTER);
view.setImageResource(mImageSource);
}
}
}
/**
* 图片的加载监听事件
*/
abstract private class OnImageLoadListener implements ZImageLoader.OnImageLoadListener
{
/**
* 实现图片显示的抽象方法
*
* @param view
* @param tag
* @param drawable
*/
abstract public void setDrawable(ImageView view, String imageUrl, String tag, Drawable drawable);
@Override
public void onSuccess(Drawable drawable, String imageUrl, String tag) {
ImageView view = (ImageView) mDiaryListView.findViewWithTag(tag == null imageUrl : tag);
this.setDrawable(view, imageUrl, tag, drawable);
}
@Override
public void onFailure(IOException e, String imageUrl, String tag) {
//Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();
}
}
// 调用该方法
ZImageLoader.getInstance().loadDrawable(item.getPicUrl(), item.getPicTag(), this.mOnPicLoadListener);
这是之前自己实现的简单的图片加载缓存类,不过今天开始尝试使用开源的《Android Universal Image Loader》,先把代码贴在这儿也算做个备份吧
github项目地址:https://github.com/nostra13/Android-Universal-Image-Loader
功能很完善、很强大了