我一直在尝试在我的应用程序中实现 LruCache,但在连接点和在不同组件之间传递位图时遇到了困难。
我想知道如何在我的应用程序中集成 LruCache。我也想了解实现 LruCache 的过程,所以越详细越好
第一类是 AsyncTask Image Loader,第二类是我的自定义适配器
异步任务
public class GetImagesBitmap extends AsyncTask<ImageView, Void, Bitmap>{
InputStream inputStream;
ImageView imageView = null;
String path;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = client.execute(httpGet);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode
+ " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = httpResponse.getEntity();
if(entity != null){
inputStream = null;
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
if(inputStream != null){
inputStream.close();
}
entity.consumeContent();
} catch (IOException e) {
httpGet.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url);
}finally{
if(client != null){
client.close();
}
}
return null;
}
}
自定义视图适配器类
public class CustomListingListAdapter extends ArrayAdapter<HashMap<String, String>> {
private ArrayList<HashMap<String, String>> data;
private Context context;
private LayoutInflater mInflater;
private int viewId;
private String[] tag;
HashMap<String, String> currentData = new HashMap<String, String>();
//private static final String TAG_CONTENT = "content";
//private static final String TAG_RATING = "rating";
private static final String TAG_NAME = "name";
private static final String TAG_IMAGE_PATH = "image_path";
private static final String TAG_PRODUCT_ID = "product_id";
private static final String TAG_RATING = "rating";
public CustomListingListAdapter(Context c,
ArrayList<HashMap<String, String>> data,
int viewId, String[] tag) {
super( c, viewId, data);
this.context = c;
this.data= data;
this.viewId = viewId ;
}
@Override
public int getCount() {
return data.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
Holder holder;
if (convertView == null) {
// Inflate the view since it does not exist
if (vi == null) {
mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = mInflater.inflate(R.layout.list_item_layout, null);
}
holder = new Holder();
holder.Name = (TextView) vi.findViewById(R.id.name);
holder.pid = (TextView) vi.findViewById(R.id.pid);
holder.imageView = (ImageView) vi.findViewById(R.id.list_image);
holder.ratingView = (ImageView) vi.findViewById(R.id.num_stars);
vi.setTag(holder);
}else {
holder = (Holder) vi.getTag();
}
currentData = (HashMap<String, String>) data.get(position);
holder.imageView.setTag(currentData.get(TAG_IMAGE_PATH));
if (currentData != null) {
holder.Name.setText(currentData.get(TAG_NAME));
holder.pid.setText(currentData.get(TAG_PRODUCT_ID));
}
new GetImagesBitmap().execute(holder.imageView);
return vi;
}
private static class Holder {
public ImageView imageView;
public ImageView ratingView;
public TextView pid;
public TextView Name;
}
}