0

我的应用程序从 Web 下载数据(图像和文本),并将其显示为 ListView。在 ListView 适配器中,我实现了内存缓存来存储位图。从 ListView 中检索图像的主要目的是在单击行后将其传递给新的 Activity。我知道我可以将图像 URL 传递给新的 Activity,然后再次下载,但我不想那样做。是否可以从 ListView 中检索位图?如果是,哪种方式最容易实施?提前致谢

编辑:

public class CinemasAdapter extends ArrayAdapter<Cinemas> {

    private ArrayList<Cinemas> movieDataItems;  
    private Activity context;

    private LinkedHashMap<String, Bitmap> bitmapCache = new LinkedHashMap<String, Bitmap>();

    public CinemasAdapter(Activity context, int textViewResourceId, ArrayList<Cinemas> movieDataItems) {
        super(context, textViewResourceId, movieDataItems);
        this.context = context;
        this.movieDataItems = movieDataItems;
    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder = new ViewHolder();
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            convertView = vi.inflate(R.layout.movie_data_row, null);
            holder.poster = (ImageView) convertView.findViewById(R.id.movie_thumb_icon);
            holder.bitmap = BitmapFactory.decodeResource(holder.poster.getResources(), R.drawable.no_image);
            convertView.setTag(holder);         
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        Cinemas movie = movieDataItems.get(position);

        if (movie!=null){
                    String url = movie.poster();

                        if (url != null) {
                            Bitmap bitmap = fetchBitmapFromCache(url);
                            if (bitmap == null) {
                                loadBitmap(url,holder.poster,holder.bitmap);
                            }
                            else {
                                holder.poster.setImageBitmap(bitmap);
                            }
                        }
                        else {
                            holder.poster.setImageResource(R.drawable.no_image);
                        }
            }
        else {
            holder.poster.setImageResource(R.drawable.no_image);
            } 

        return convertView;

    }           

    private void addBitmapToCache(String url, Bitmap bitmap) {

        if (bitmap != null) {
            synchronized (bitmapCache) {
                bitmapCache.put(url, bitmap);
            }
        }
    } 

    private Bitmap fetchBitmapFromCache(String url) {
        synchronized (bitmapCache) {
            final Bitmap bitmap = bitmapCache.get(url);
             if (bitmap != null) {
                bitmapCache.remove(url);
                bitmapCache.put(url, bitmap);
                return bitmap;
            } 
        }
        return null;
    } 

    private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {

        private String url;
        private final WeakReference<ImageView> imageViewReference;
        private Cinemas movie;

        public BitmapDownloaderTask(ImageView imageView, Cinemas mov) {
            imageViewReference = new WeakReference<ImageView>(imageView);   
            movie = mov;
        }

        @Override
        protected Bitmap doInBackground (String... source) {
            url = source[0];
            Bitmap image;
            try{
                image = BitmapFactory.decodeStream(new URL(url).openConnection().getInputStream());
                return image;
                }
            catch(Exception e){Log.e("Error", e.getMessage()); e.printStackTrace();}
            return null;
            } 


         @Override
        protected void onPostExecute(Bitmap bitmap) { 
            if (isCancelled()) {
                 bitmap = null;
             }
            if (imageViewReference != null && bitmap != null) {
                 final ImageView imageView = imageViewReference.get();
                 final BitmapDownloaderTask bitmapWorkerTask =
                         getBitmapWorkerTask(imageView);
                 if (this == bitmapWorkerTask && imageView != null) {
                     imageView.setImageBitmap(bitmap);
                     addBitmapToCache(url);
                     movie.setBitmap(bitmap);
                 }
             }
        } 
    } 

    private static class ViewHolder{
    ImageView poster;
    Bitmap bitmap;
}

    static class AsyncDrawable extends BitmapDrawable {
    private final WeakReference<BitmapDownloaderTask> bitmapWorkerTaskReference;

    public AsyncDrawable(Resources res, Bitmap bitmap,
            BitmapDownloaderTask bitmapWorkerTask) {
            super(res, bitmap);
            bitmapWorkerTaskReference = new WeakReference<BitmapDownloaderTask>(bitmapWorkerTask);
    }

    public BitmapDownloaderTask getBitmapWorkerTask() {
        return bitmapWorkerTaskReference.get();
    }
}

    private static BitmapDownloaderTask getBitmapWorkerTask(ImageView imageView) {
       if (imageView != null) {
           final Drawable drawable = imageView.getDrawable();
           if (drawable instanceof AsyncDrawable) {
               final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
               return asyncDrawable.getBitmapWorkerTask();
           }
        }
        return null;
    }

    public static boolean cancelPotentialWork(String data, ImageView imageView) {
    final BitmapDownloaderTask bitmapWorkerTask = getBitmapWorkerTask(imageView);

    if (bitmapWorkerTask != null) {
        final String bitmapData = bitmapWorkerTask.url;
        if (bitmapData != data) {
            bitmapWorkerTask.cancel(true);
        } else {
            return false;
        }
    }
    return true;
}

    public void loadBitmap(String resId, ImageView imageView, Bitmap bitmap) {
    if (cancelPotentialWork(resId, imageView)) {
        final BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
        final AsyncDrawable asyncDrawable = new AsyncDrawable(imageView.getResources(), bitmap, task);
        imageView.setImageDrawable(asyncDrawable);
        task.execute(resId);
    }
}
}
4

1 回答 1

0

这将为您从 Imageview 中获取位图。

image_view.buildDrawingCache();
Bitmap bmap = image_view.getDrawingCache();

现在您可以将位图发送到另一个活动,如下所示:

Bitmap 实现 Parcelable,因此您始终可以在意图中传递它:

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bmap);

并在另一端检索它:

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
于 2013-03-16T16:40:32.733 回答