/** * 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) } }
/** * 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#setImageCache(ImageCache)}. 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. * @param isFromDisk TODO */ public void loadImage(Object data, ImageView imageView, int width, boolean isFromDisk) { Bitmap bitmap = null; if (isFromDisk) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), mLoadingBitmap, task); imageView.setImageDrawable(asyncDrawable); task.execute(data, width); } else { if (mImageCache != null) { bitmap = mImageCache.getBitmapFromMemCache(String.valueOf(data)); } if (bitmap != null) { // Bitmap found in memory cache imageView.setImageBitmap(zoomImg(bitmap, width)); } else if (cancelPotentialWork(data, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), mLoadingBitmap, task); imageView.setImageDrawable(asyncDrawable); task.execute(data, width); } } }
public void loadBitmap(int resId, ImageView imageView) { if (cancelPotentialWork(resId, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); imageView.setBackgroundResource(R.drawable.alexander_rossi_pilot); task.execute(resId); } }
/** * 加载网络图片到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); } }
// Called when a new Loader needs to be created public AsyncTaskLoader<List<LCBOEntity>> onCreateLoader(int id, Bundle args) { if (lastResult.image_thumb_url == null || lastResult.image_thumb_url.isEmpty()) { BitmapWorkerTask task = new BitmapWorkerTask(this, lastResult.itemNumber); task.execute(lastResult.itemNumber); } if (MainActivity.mCurrentLocation != null) { spinner.setVisibility(View.VISIBLE); LocationFetchTask locTask = new LocationFetchTask( MainActivity.mCurrentLocation, lastResult.itemNumber, new LocationFetchTask.LocationListener() { @Override public void onLocationsFetched(List<LCBOEntity> locations) { lastLocations = locations; addLastLocationsData(); spinner.setVisibility(View.GONE); } }); locTask.execute(MainActivity.mCurrentLocation); } return new LCBOAPILoader(this, args, LCBOAPIParser.QueryType.kProductDetail); }
public void loadBitmap(Message message, ImageView imageView) { Bitmap bm; try { bm = xmppConnectionService .getFileBackend() .getThumbnail(message, (int) (metrics.density * 288), true); } catch (FileNotFoundException e) { bm = null; } if (bm != null) { imageView.setImageBitmap(bm); imageView.setBackgroundColor(0x00000000); } else { if (cancelPotentialWork(message, imageView)) { imageView.setBackgroundColor(0xff333333); final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(getResources(), null, task); imageView.setImageDrawable(asyncDrawable); try { task.execute(message); } catch (final RejectedExecutionException ignored) { } } } }
/** 取消所有正在下载或等待下载的任务。 */ public void cancelAllTasks() { if (taskCollection != null) { for (BitmapWorkerTask task : taskCollection) { task.cancel(false); } } }
/** * 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); } } }
/** * 取消这个imageview所对应的任务 * * @param imageView */ public static void cancelWork(ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { bitmapWorkerTask.cancel(true); final Object bitmapData = bitmapWorkerTask.mData; Ioc.getIoc().getLogger().d("cancelWork - cancelled work for " + bitmapData); } }
private void loadBitmap(int resId, ImageView imageView) { if (cancelPotentialWork(resId, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(context.getResources(), null, task); imageView.setImageDrawable(asyncDrawable); task.execute(resId); } }
/** * 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); } }
/** * Cancels any pending work attached to the provided ImageView. * * @param imageView */ public static void cancelWork(ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { bitmapWorkerTask.cancel(true); if (BuildConfig.DEBUG) { LOGD(TAG, "cancelWork - cancelled work for " + bitmapWorkerTask.data); } } }
public void loadBitmap(String path, ImageView imageView, String name) { if (BitmapWorkerTask.cancelPotentialWork(path, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(imageView, name); Bitmap bm = BitmapTools.decodeBitmapFromResource(context.getResources(), R.drawable.white, 200, 240); final AsyncDrawable asyncDrawable = new AsyncDrawable(context.getResources(), bm, task); imageView.setImageDrawable(asyncDrawable); task.execute(path); } }
/** * Cancels any pending work attached to the provided ImageView. * * @param imageView */ public static void cancelWork(ImageViewWithProcessIndicator imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { bitmapWorkerTask.cancel(true); if (BuildConfig.DEBUG) { final Object bitmapData = bitmapWorkerTask.data; Log.d(TAG, "cancelWork - cancelled work for " + bitmapData); } } }
/** * Cancels any pending work attached to the provided ImageView. * * @param imageView */ public static void cancelWork(ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { bitmapWorkerTask.cancel(true); if (Constants.DEBUG) { final Object bitmapData = bitmapWorkerTask.mData; Log.d(TAG, "cancelWork - cancelled work for " + bitmapData); } } }
public void loadImage(Object data, ImageSwitcher imageSwitcher, boolean fullScale, int width) { final BitmapWorkerTask task = new BitmapWorkerTask(imageSwitcher); final AsyncDrawable asyncDrawable = new AsyncDrawable(context.getResources(), loadingBitmap, task); imageSwitcher.setImageDrawable(asyncDrawable); task.fullScale = fullScale; task.width = width; task.imageSwitcher = imageSwitcher; task.execute(data); }
public static void loadBitmap( ViewerFragment hostFragment, String key, int position, ImageView imageView) { if (cancelPotentialWork(key, imageView)) { final BitmapWorkerTask task = new BitmapWorkerTask(hostFragment, imageView); final AsyncDrawable asyncDrawable = new AsyncDrawable(hostFragment.getResources(), hostFragment.mPlaceHolderBitmap, task); imageView.setImageDrawable(asyncDrawable); task.execute(key, position); } }
public static boolean cancelPotentialWork(Message message, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Message oldMessage = bitmapWorkerTask.message; if (oldMessage == null || message != oldMessage) { bitmapWorkerTask.cancel(true); } else { return false; } } return true; }
/** * Returns true if the current work has been canceled or if there was no work in progress on this * image view. Returns false if the work in progress deals with the same data. The work is not * stopped in that case. */ public static final boolean executePotentialWork(final Object data, final ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Object bitmapData = bitmapWorkerTask.mKey; if (bitmapData == null || !bitmapData.equals(data)) { bitmapWorkerTask.cancel(true); } else { // The same work is already in progress return false; } } return true; }
public static boolean cancelPotentialWork(int data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final int bitmapData = bitmapWorkerTask.data; if (bitmapData != data) { bitmapWorkerTask.cancel(true); } else { return false; } } return true; }
public boolean cancelPotentialWork(String filePath, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final String bitmapFilePath = bitmapWorkerTask.filePath; if (bitmapFilePath != null && !bitmapFilePath.equalsIgnoreCase(filePath)) { bitmapWorkerTask.cancel(true); } else { return false; } } return true; }
private void loadBitmaps(int position, ImageView imageView) { ImgInfo data = imginfos.getItemAt(position); Bitmap bitmap = getBitmapFromMemoryCache(String.valueOf(data)); if (bitmap == null) { imageView.setImageResource(R.drawable.empty_photo); BitmapWorkerTask task = new BitmapWorkerTask(); taskCollection.add(task); task.execute(position); } else { if (imageView != null && bitmap != null) { imageView.setImageBitmap(bitmap); } } }
/** * 异步加载图片,防止并发 * * @param mContext * @param resId * @param imageView * @param reqHeight * @param reqWidth */ public static void loadAsyncBitmap( Context mContext, int resId, ImageView imageView, int reqWidth, int reqHeight) { if (cancelPotentialWork(resId, imageView)) { Bitmap placeHolder = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.icon_geo); final BitmapWorkerTask task = new BitmapWorkerTask(mContext, imageView); task.reqWidth = reqWidth; task.reqHeight = reqHeight; final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), placeHolder, task); imageView.setImageDrawable(asyncDrawable); task.execute(resId); } }
/** * Returns true if the current work has been canceled or if there was no work in progress on this * image view. Returns false if the work in progress deals with the same data. The work is not * stopped in that case. */ public static boolean cancelPotentialWork(Object data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Object bitmapData = bitmapWorkerTask.data; if (bitmapData == null || !bitmapData.equals(data)) { bitmapWorkerTask.cancel(true); LOGV(TAG, "cancelPotentialWork - cancelled work for " + data); } else { // The same work is already in progress. return false; } } return true; }
public static boolean cancelPotentialWork(int data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final int bitmapData = bitmapWorkerTask.data; if (bitmapData == 0 || bitmapData != data) { // 新的任务和之前的不同,则取消之前的任务 bitmapWorkerTask.cancel(true); } else { // 相同的任务已经在执行中 return false; } } // 没有任务和此控件关联,或者一个已经存在的已经被取消 return true; }
/** * * * <h1>第一步</h1> * * 根据imageView获取到AsyncDrawable<br> * * <h1>第二步</h1> * * 从AsyncDrawable获得BitmapWorkerTask<br> * * <h1>第三步</h1> * * 根据BitmapWorkerTask中的url判断当前下载的url和之前是否一样<br> * 如果一样则返回false无需再下载<br> * * <h1>第四步</h1> * * 如果不一样则说明 当前imageview需要显示的图片和之前图片不一样,取消之前下载进程 返回true */ public static boolean cancelPotentialWork(Object data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Object bitmapData = bitmapWorkerTask.mData; // 判断当前的Url和之前的url是否一样 if (bitmapData == null || !bitmapData.equals(data)) { bitmapWorkerTask.cancel(true); Ioc.getIoc().getLogger().d("cancelPotentialWork - cancelled work for " + data); } else { return false; } } return true; }
/** * Returns true if the current work has been canceled or if there was no work in progress on this * image view. Returns false if the work in progress deals with the same data. The work is not * stopped in that case. */ public static boolean cancelPotentialWork(Object data, ImageView imageView) { // BEGIN_INCLUDE(cancel_potential_work) final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final Object bitmapData = bitmapWorkerTask.mData; if (bitmapData == null || !bitmapData.equals(data)) { bitmapWorkerTask.cancel(true); } else { // The same work is already in progress. return false; } } return true; // END_INCLUDE(cancel_potential_work) }
public static boolean cancelPotentialWork(int data, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final int bitmapData = bitmapWorkerTask.data; if (bitmapData != data) { // Cancel previous task bitmapWorkerTask.cancel(true); } else { // The same work is already in progress return false; } } // No task associated with the ImageView, or an existing task was // cancelled return true; }
public static boolean cancelPotentialWork(String key, ImageView imageView) { final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView); if (bitmapWorkerTask != null) { final String bitmapKey = bitmapWorkerTask.key; if (bitmapKey != key) { // Cancel previous task Savelog.d(TAG, debug, "Canceling task for key:" + key); bitmapWorkerTask.cancel(true); } else { // The same work is already in progress return false; } } // No task associated with the ImageView, or an existing task was cancelled return true; }