我下载了图像并使用 Fedora 的惰性列表将它们缓存到我的缓存文件夹中,但现在,而不是将它们显示到列表视图中。我需要使用 viewpager + touchimageview。我已经找到了示例,但不知道如何将它们加载到 AndroidTouchGallery 中。有人可以给我指点吗?
这是文件cache.java
package com.fedorvlasov.lazylist;
import java.io.File;
import java.net.URLEncoder;
import android.content.Context;
public class FileCache {
private File cacheDir;
public FileCache(Context context){
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url){
//I identify images by hashcode. Not a perfect solution, good for the demo.
//String filename=String.valueOf(url.hashCode());
//Another possible solution (thanks to grantland)
String filename = URLEncoder.encode(url);
File f = new File(cacheDir, filename);
return f;
}
public void clear(){
File[] files=cacheDir.listFiles();
if(files==null)
return;
for(File f:files)
f.delete();
}
}
内存缓存(也由 fedora 提供)
package com.fedorvlasov.lazylist;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import android.graphics.Bitmap;
import android.util.Log;
public class MemoryCache {
private static final String TAG = "MemoryCache";
private Map<String, Bitmap> cache=Collections.synchronizedMap(
new LinkedHashMap<String, Bitmap>(10,1.5f,true));//Last argument true for LRU ordering
private long size=0;//current allocated size
private long limit=1000000;//max memory in bytes
public MemoryCache(){
//use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory()/4);
}
public void setLimit(long new_limit){
limit=new_limit;
Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");
}
public Bitmap get(String id){
try{
if(!cache.containsKey(id))
return null;
//NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78
return cache.get(id);
}catch(NullPointerException ex){
ex.printStackTrace();
return null;
}
}
public void put(String id, Bitmap bitmap){
try{
if(cache.containsKey(id))
size-=getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size+=getSizeInBytes(bitmap);
checkSize();
}catch(Throwable th){
th.printStackTrace();
}
}
private void checkSize() {
Log.i(TAG, "cache size="+size+" length="+cache.size());
if(size>limit){
Iterator<Entry<String, Bitmap>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated
while(iter.hasNext()){
Entry<String, Bitmap> entry=iter.next();
size-=getSizeInBytes(entry.getValue());
iter.remove();
if(size<=limit)
break;
}
Log.i(TAG, "Clean cache. New size "+cache.size());
}
}
public void clear() {
try{
//NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78
cache.clear();
size=0;
}catch(NullPointerException ex){
ex.printStackTrace();
}
}
long getSizeInBytes(Bitmap bitmap) {
if(bitmap==null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}
这是我应该将缓存的数据加载到触摸库中的部分。以前我只需要加载一个字符串数组(数据存储在 sdcard 中),现在我如何在 touchgallery 中显示我作为缓存下载的项目?
public class UrlTouchImageView extends RelativeLayout {
protected ProgressBar mProgressBar;
protected TouchImageView mImageView;
protected Context mContext;
UrlPagerAdapter pagerAdapter;
String[] mImageIds;
Bitmap bm;
ArrayList<String> mStringList= new ArrayList<String>();
public UrlTouchImageView(Context ctx, UrlPagerAdapter pagerAdapter)
{
super(ctx);
mContext = ctx;
this.pagerAdapter = pagerAdapter;
init();
}
public UrlTouchImageView(Context ctx, AttributeSet attrs)
{
super(ctx, attrs);
mContext = ctx;
init();
}
public TouchImageView getImageView()
{
return mImageView;
}
protected void init() {
mImageView = new TouchImageView(mContext);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
mImageView.setLayoutParams(params);
this.addView(mImageView);
mImageView.setVisibility(GONE);
mProgressBar = new ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal);
params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.setMargins(30, 0, 30, 0);
mProgressBar.setLayoutParams(params);
mProgressBar.setIndeterminate(true);
this.addView(mProgressBar);
}
public void setUrl(String imageUrl)
{
new ImageLoadTask().execute(imageUrl);
}
//No caching load
public class ImageLoadTask extends AsyncTask<String, Integer, Bitmap>
{
//RETRIEVES LINK FROM GALLERYACTIVITY
//READ MNT/SDCARD/....
@Override
protected Bitmap doInBackground(String... strings) {
bm = null;
String url = strings[0];
//Log.d("url",url);
bm = BitmapFactory.decodeFile(url);
return bm;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
mImageView.setImageBitmap(bm);
mImageView.setVisibility(VISIBLE);
mProgressBar.setVisibility(GONE);
}
}
}
我很抱歉我的英语不好以及对android编程的了解不足。希望大家能给我建议。。