/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.bitmapfun.util;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
import com.example.android.bitmapfun.BuildConfig;
import java.lang.ref.WeakReference;
/**
* This class wraps up completing some arbitrary long running work when loading a bitmap to an
* ImageView. It handles things like using a memory and disk cache, running the work in a background
* thread and setting a placeholder image.
*/
public abstract class ImageWorker {
private static final String TAG = "ImageWorker";
private static final int FADE_IN_TIME = 200;
private ImageCache mImageCache;
private Bitmap mLoadingBitmap;
private boolean mFadeInBitmap = true;
private boolean mExitTasksEarly = false;
protected Context mContext;
protected ImageWorkerAdapter mImageWorkerAdapter;
protected ImageWorker(Context context) {
mContext = context;
}
/**
* 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.
*/
public void loadImage(Object data, ImageView imageView) {
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(mContext.getResources(), mLoadingBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(data);
}
}
/**
* Load an image specified from a set adapter 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. {@link ImageWorker#setAdapter(ImageWorkerAdapter)} must be called before using this
* method.
*
* @param data The URL of the image to download.
* @param imageView The ImageView to bind the downloaded image to.
*/
public void loadImage(int num, ImageView imageView) {
if (mImageWorkerAdapter != null) {
loadImage(mImageWorkerAdapter.getItem(num), imageView);
} else {
throw new NullPointerException("Data not set, must call setAdapter() first.");
}
}
/**
* Set placeholder bitmap that shows when the the background thread is running.
*
* @param bitmap
*/
public void setLoadingImage(Bitmap bitmap) {
mLoadingBitmap = bitmap;
}
/**
* Set placeholder bitmap that shows when the the background thread is running.
*
* @param resId
*/
public void setLoadingImage(int resId) {
mLoadingBitmap = BitmapFactory.decodeResource(mContext.getResources(), resId);
}
/**
* Set the {@link ImageCache} object to use with this ImageWorker.
*
* @param cacheCallback
*/
public void setImageCache(ImageCache cacheCallback) {
mImageCache = cacheCallback;
}
public ImageCache getImageCache() {
return mImageCache;
}
/**
* If set to true, the image will fade-in once it has been loaded by the background thread.
*
* @param fadeIn
*/
public void setImageFadeIn(boolean fadeIn) {
mFadeInBitmap = fadeIn;
}
public void setExitTasksEarly(boolean exitTasksEarly) {
mExitTasksEarly = exitTasksEarly;
}
/**
* Subclasses should override this to define any processing or work that must happen to produce
* the final bitmap. This will be executed in a background thread and be long running. For
* example, you could resize a large bitmap here, or pull down an image from the network.
*
* @param data The data to identify which image to process, as provided by
* {@link ImageWorker#loadImage(Object, ImageView)}
* @return The processed bitmap
*/
protected abstract Bitmap processBitmap(Object data);
public static void cancelWork(ImageView 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);
}
}
}
/**
* 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);
if (BuildConfig.DEBUG) {
Log.d(TAG, "cancelPotentialWork - cancelled work for " + data);
}
} else {
// The same work is already in progress.
return false;
}
}
return true;
}
/**
* @param imageView Any imageView
* @return Retrieve the currently active work task (if any) associated with this imageView.
* null if there is no such task.
*/
private static BitmapWorkerTask 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;
}
/**
* The actual AsyncTask that will asynchronously process the image.
*/
private class BitmapWorkerTask extends AsyncTask<
没有合适的资源?快使用搜索试试~ 我知道了~
Android--开发--BitmapFunc.rar
共76个文件
class:34个
java:14个
xml:10个
需积分: 5 0 下载量 123 浏览量
2024-04-10
20:16:17
上传
评论
收藏 691KB RAR 举报
温馨提示
Android--开发--BitmapFunc
资源推荐
资源详情
资源评论
收起资源包目录
Android--开发--BitmapFunc.rar (76个子文件)
Android--开发--BitmapFunc
BitmapFunc
BitmapFun
project.properties 562B
.classpath 357B
assets
src
com
example
android
bitmapfun
provider
Images.java 11KB
ui
ImageDetailFragment.java 4KB
ImageGridFragment.java 12KB
ImageDetailActivity.java 8KB
ImageGridActivity.java 1KB
util
Utils.java 4KB
ImageCache.java 8KB
ImageWorker.java 13KB
ImageFetcher.java 6KB
DiskLruCache.java 11KB
ImageResizer.java 7KB
RetainFragment.java 3KB
libs
android-support-v4.jar 265KB
res
drawable-nodpi
empty_photo.png 3KB
drawable-mdpi
ic_launcher.png 5KB
drawable-xhdpi
ic_launcher.png 14KB
values-large
dimens.xml 779B
menu
main_menu.xml 270B
drawable-ldpi
ic_launcher.png 3KB
values-xlarge
dimens.xml 779B
values
dimens.xml 836B
strings.xml 1KB
styles.xml 989B
layout
image_detail_pager.xml 886B
image_detail_fragment.xml 1KB
image_grid_fragment.xml 1KB
drawable-hdpi
ic_launcher.png 9KB
bin
classes
com
example
android
bitmapfun
R$style.class 510B
R$string.class 599B
provider
Images.class 10KB
Images$2.class 811B
Images$1.class 806B
R$layout.class 529B
R$id.class 586B
BuildConfig.class 367B
R$attr.class 373B
R$drawable.class 475B
R$dimen.class 536B
ui
ImageDetailActivity$1.class 1KB
ImageGridActivity.class 1KB
ImageDetailFragment.class 3KB
ImageDetailActivity$ImagePagerAdapter.class 2KB
ImageGridFragment$1.class 2KB
ImageGridFragment.class 7KB
ImageDetailActivity.class 6KB
ImageGridFragment$ImageAdapter.class 4KB
R.class 783B
util
ImageCache$1.class 1KB
RetainFragment.class 2KB
ImageWorker$ImageWorkerAdapter.class 536B
ImageCache.class 5KB
DiskLruCache$1.class 747B
Utils.class 3KB
ImageResizer.class 3KB
ImageFetcher.class 5KB
ImageWorker.class 7KB
ImageWorker$AsyncDrawable.class 1KB
ImageWorker$BitmapWorkerTask.class 3KB
DiskLruCache.class 8KB
ImageCache$ImageCacheParams.class 1KB
R$menu.class 426B
resources.ap_ 39KB
jarlist.cache 119B
res
drawable-nodpi
empty_photo.png 1KB
drawable-mdpi
ic_launcher.png 5KB
drawable-xhdpi
ic_launcher.png 14KB
drawable-ldpi
ic_launcher.png 3KB
drawable-hdpi
ic_launcher.png 9KB
classes.dex 300KB
BitmapFun.apk 168KB
.project 845B
AndroidManifest.xml 2KB
gen
com
example
android
bitmapfun
BuildConfig.java 171B
R.java 2KB
共 76 条
- 1
资源评论
温柔-的-女汉子
- 粉丝: 964
- 资源: 4009
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功