Jamendo学习笔记之五:Adpter框架
2012-05-22 15:52:43 来源:WEB开发网核心提示:i = new RemoteImageView(mContext); i.setScaleType(RemoteImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new Gallery.LayoutParams(mIconSize, mIconSize)); } el
i = new RemoteImageView(mContext);
i.setScaleType(RemoteImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new Gallery.LayoutParams(mIconSize, mIconSize));
} else {
i = (RemoteImageView) convertView;
}
i.setDefaultImage(R.drawable.no_cd);
i.setImageUrl(mList.get(position).getImage());
return i;
}
3, 显然2中,Jamendo自定义了一个RemoteImageView,用来实现图片的下载和显示。
setImageUrl()的代码如下:
public void setImageUrl(String url){
// Log.d("img_url", "img_url is :" + url);
if (mUrl != null && mUrl.equals(url) && (mCurrentlyGrabbedUrl == null ||//1:url赋给全局变量mUrl,两者相等且都为空,但是未执行,所以mCurrentlyGrabbedUrl为空
(mCurrentlyGrabbedUrl != null && !mCurrentlyGrabbedUrl.equals(url)))) {//2:第n(n>1)次执行时,并未完成downloadTask
mFailure++;
if(mFailure > MAX_FAILURES){//超过指定重连次数
Log.e(JamendoApplication.TAG, "Failed to download "+url+", falling back to default image");
loadDefaultImage();
return;
}
} else {
mUrl = url;
mFailure = 0;
}
updateCacheSize();
if (mCacheSize>0 && (url.contains(ALBUMS) || url.contains(RADIOS))) {//只有两类路径图片需要缓存
String fileName = convertUrlToFileName(url);
String dir = getDirectory(fileName);
Log.d("img_url", "dir is :" + dir);
String pathFileName = dir + "/" + fileName;
Log.d("img_url", "pathFileName is :" + pathFileName);
Bitmap tbmp = BitmapFactory.decodeFile(pathFileName);//从指定文件保存路径解码处图片
if (tbmp == null) {
Log.d(JamendoApplication.TAG, "Image is not present, try to download");
try{
new DownloadTask().execute(url);
} catch (RejectedExecutionException e) {
// do nothing, just don't crash
}
} else {
Log.i(JamendoApplication.TAG, "Loading album cover from file");
this.setImageBitmap(tbmp);
updateFileTime(dir,fileName );
}
removeAlbumCoversCache(dir, fileName);//对专辑图片的缓存处理:比较大120k
removeRadioCoversCache(dir, fileName);//对广播图片的缓存处理:比较小8k
}
else {
Log.i(JamendoApplication.TAG, "File not cached supported" + url);
ImageCache imageCache = JamendoApplication.getInstance()
.getImageCache();
if (imageCache.isCached(url)) {
this.setImageBitmap(imageCache.get(url));
} else {
try {
Log.i(JamendoApplication.TAG, "Image is not present, try to download");
new DownloadTask().execute(url);
} catch (RejectedExecutionException e) {
// do nothing, just don't crash
}
}
}
}
依照顺序,分析如下:
1st. 重连的判断
指定重连次数,不断请求,大于重连次数,设置为默认图片
2nd. 更新设置中的缓存大小
updateCacheSize();
3rd. 设置图片(先从缓存中找,没有再找SDCard,最后再下载)
缓存的问题将单独写一篇文章
至此:TitleBar的相关Adapter解析完毕。给人印象最深的仍然是面向对象的思想。A: 继承BaseAdapter所实现的4个方法中,3个都基本一样,所以构建父类 ArrayListAdapter。子类.
B: 封装RemoteImageView,可移植性增强。
i.setScaleType(RemoteImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new Gallery.LayoutParams(mIconSize, mIconSize));
} else {
i = (RemoteImageView) convertView;
}
i.setDefaultImage(R.drawable.no_cd);
i.setImageUrl(mList.get(position).getImage());
return i;
}
3, 显然2中,Jamendo自定义了一个RemoteImageView,用来实现图片的下载和显示。
setImageUrl()的代码如下:
public void setImageUrl(String url){
// Log.d("img_url", "img_url is :" + url);
if (mUrl != null && mUrl.equals(url) && (mCurrentlyGrabbedUrl == null ||//1:url赋给全局变量mUrl,两者相等且都为空,但是未执行,所以mCurrentlyGrabbedUrl为空
(mCurrentlyGrabbedUrl != null && !mCurrentlyGrabbedUrl.equals(url)))) {//2:第n(n>1)次执行时,并未完成downloadTask
mFailure++;
if(mFailure > MAX_FAILURES){//超过指定重连次数
Log.e(JamendoApplication.TAG, "Failed to download "+url+", falling back to default image");
loadDefaultImage();
return;
}
} else {
mUrl = url;
mFailure = 0;
}
updateCacheSize();
if (mCacheSize>0 && (url.contains(ALBUMS) || url.contains(RADIOS))) {//只有两类路径图片需要缓存
String fileName = convertUrlToFileName(url);
String dir = getDirectory(fileName);
Log.d("img_url", "dir is :" + dir);
String pathFileName = dir + "/" + fileName;
Log.d("img_url", "pathFileName is :" + pathFileName);
Bitmap tbmp = BitmapFactory.decodeFile(pathFileName);//从指定文件保存路径解码处图片
if (tbmp == null) {
Log.d(JamendoApplication.TAG, "Image is not present, try to download");
try{
new DownloadTask().execute(url);
} catch (RejectedExecutionException e) {
// do nothing, just don't crash
}
} else {
Log.i(JamendoApplication.TAG, "Loading album cover from file");
this.setImageBitmap(tbmp);
updateFileTime(dir,fileName );
}
removeAlbumCoversCache(dir, fileName);//对专辑图片的缓存处理:比较大120k
removeRadioCoversCache(dir, fileName);//对广播图片的缓存处理:比较小8k
}
else {
Log.i(JamendoApplication.TAG, "File not cached supported" + url);
ImageCache imageCache = JamendoApplication.getInstance()
.getImageCache();
if (imageCache.isCached(url)) {
this.setImageBitmap(imageCache.get(url));
} else {
try {
Log.i(JamendoApplication.TAG, "Image is not present, try to download");
new DownloadTask().execute(url);
} catch (RejectedExecutionException e) {
// do nothing, just don't crash
}
}
}
}
依照顺序,分析如下:
1st. 重连的判断
指定重连次数,不断请求,大于重连次数,设置为默认图片
2nd. 更新设置中的缓存大小
updateCacheSize();
3rd. 设置图片(先从缓存中找,没有再找SDCard,最后再下载)
缓存的问题将单独写一篇文章
至此:TitleBar的相关Adapter解析完毕。给人印象最深的仍然是面向对象的思想。A: 继承BaseAdapter所实现的4个方法中,3个都基本一样,所以构建父类 ArrayListAdapter。子类.
B: 封装RemoteImageView,可移植性增强。
更多精彩
赞助商链接