/** * Load an image specified by the data parameter into an ImageView (override {@link * ImageWorker#processBitmap(Object)} to define the processing logic). A memory and disk cache * will be used if an {@link ImageCache} has been added using {@link * ImageWorker#addImageCache(android.support.v4.app.FragmentManager, * ImageCache.ImageCacheParams)}. If the image is found in the memory cache, it is set * immediately, otherwise an {@link AsyncTask} will be created to asynchronously load the bitmap. * * @param data The URL of the image to download. * @param imageView The ImageView to bind the downloaded image to. */ public void loadImage(Object data, ImageView imageView) { if (data == null) { return; } BitmapDrawable value = null; if (mImageCache != null) { value = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (value != null) { // Bitmap found in memory cache imageView.setImageDrawable(value); } else if (cancelPotentialWork(data, imageView)) { // BEGIN_INCLUDE(execute_background_task) final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task); imageView.setImageDrawable(asyncDrawable); // NOTE: This uses a custom version of AsyncTask that has been pulled from the // framework and slightly modified. Refer to the docs at the top of the class // for more info on what was changed. task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR); // END_INCLUDE(execute_background_task) } }
/** * 加载网络图片到ImageView上(当然也可以重写{@link ImageWorker#processBitmap(Object)}来实现图片获取过程)<br> * 如果图片在内存和本地缓存ImageWorker中存在,则直接 <br> * 否则将开启{@link AsyncTask} 下载图片 * * @param data 图片下载链接 * @param imageView 图片显示组件 */ public void loadImage(Object data, ImageView imageView) { if (data == null) { return; } if (data.toString().indexOf("?") != -1) { data = data + "&w=" + getW() + "&h=" + getH(); } else { data = data + "?w=" + getW() + "&h=" + getH(); } BitmapDrawable value = null; mImageCache = ImageLoadManager.instance().getmImageCache(); if (mImageCache != null) { value = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } // 如果图片为空 if (value != null) { // 如果图片不为空 则直接设置 if (value.getBitmap() != null) { finish(value.getBitmap(), imageView, lister); } imageView.setImageDrawable(value); } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(data, imageView, lister); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task); imageView.setImageDrawable(asyncDrawable); // 通过线程池执行下载进程 task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR); } }
/** * Load an image specified by the data parameter into an ImageView (override {@link * ImageWorker#processBitmap(Object)} to define the processing logic). A memory and disk cache * will be used if an {@link ImageCache} has been added using {@link * ImageWorker#addImageCache(FragmentManager, ImageCache.ImageCacheParams)}. If the image is found * in the memory cache, it is set immediately, otherwise an {@link AsyncTask} will be created to * asynchronously load the bitmap. * * @param data The URL of the image to download. * @param imageView The ImageView to bind the downloaded image to. */ public void loadImage(Object data, ImageView imageView) { if (data == null) { return; } // LogUtil.d("sd card space", String.valueOf(DeviceUtil.getInternalStorageSpace())); if (DeviceUtil.getInternalStorageSpace() < 50 && System.currentTimeMillis() - preToastTime < 10000) { preToastTime = System.currentTimeMillis(); Toast.makeText(context, R.string.bfgame_sdcard_Space_full, Toast.LENGTH_SHORT).show(); } BitmapDrawable value = null; if (mImageCache != null) { value = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (value != null) { // Bitmap found in memory cache imageView.setImageDrawable(value); } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, mLoadingBitmap, task, defaultRsId); imageView.setImageDrawable(asyncDrawable); imageView.setBackgroundResource(defaultRsId); // imageView.setTag(R.id.cache_tag, asyncDrawable); // NOTE: This uses a custom version of AsyncTask that has been pulled from the // framework and slightly modified. Refer to the docs at the top of the class // for more info on what was changed. task.executeOnExecutor(AsyncTask.DUAL_THREAD_EXECUTOR, data); } }
/** * Load an image specified by the data parameter into an ImageView (override {@link * ImageWorker#processBitmap(Object)} to define the processing logic). A memory and disk cache * will be used if an {@link ImageCache} has been set using {@link ImageWorker#addImageCache}. If * the image is found in the memory cache, it is set immediately, otherwise an {@link AsyncTask} * will be created to asynchronously load the bitmap. * * @param data The URL of the image to download. * @param imageView The ImageView to bind the downloaded image to. */ public void loadImage(Object data, ImageView imageView, Bitmap loadingBitmap) { if (data == null) { return; } Bitmap bitmap = null; if (mImageCache != null) { bitmap = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (bitmap != null) { // Bitmap found in memory cache imageView.setImageBitmap(bitmap); } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mResources, loadingBitmap, task); imageView.setImageDrawable(asyncDrawable); if (UIUtils.hasHoneycomb()) { // On HC+ we execute on a dual thread executor. There really isn't much extra // benefit to having a really large pool of threads. Having more than one will // likely benefit network bottlenecks though. task.executeOnExecutor(DUAL_THREAD_EXECUTOR, data); } else { // Otherwise pre-HC the default is a thread pool executor (not ideal, serial // execution or a smaller number of threads would be better). task.execute(data); } } }
/** * Executes the given {@link BitmapWorkerTask} in parallel. On Honeycomb the default executor is a * serial executor so we explicitly use a parallel (thread pool) executor. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void executeTaskInParallel(Object data, BitmapWorkerTask task) { if (EtcUtils.hasHoneycomb()) { // Execute in parallel task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, data); } else { task.execute(data); } }
@SuppressLint("NewApi") public void loadThumbnailBitmap(FeedImage image, ImageView target) { if (image != null) { Bitmap bitmap = getBitmapFromThumbnailCache(image.getId()); if (bitmap != null) { target.setImageBitmap(bitmap); } else { target.setImageResource(R.drawable.default_cover); BitmapWorkerTask worker = new BitmapWorkerTask(target, image, LENGTH_BASE_THUMBNAIL); if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { worker.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { worker.execute(); } } } else { target.setImageResource(R.drawable.default_cover); } }