我使用过的代码 the Android documentation并且只有一件事似乎无法发挥作用。我无法在bitmapWorkerTask上执行int数据,因为它无法解析符号。我的方法:
public boolean cancelPotentialWork(int data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final int bitmapData = bitmapWorkerTask.data; //this is the line that gives me the error
// If bitmapData is not yet set or it differs from the new data
if (bitmapData == 0 || 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;
}
如有必要,我还会提供其他方法:
class AsyncDrawable extends BitmapDrawable {
objectAdapter oa;
Bitmap bm;
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
private int data;
private ImageView imageView;
public AsyncDrawable(Resources res, Bitmap bitmap,
BitmapWorkerTask bitmapWorkerTask) {
super(res, bitmap);
bitmapWorkerTaskReference =
new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask() {
return bitmapWorkerTaskReference.get();
}
public void loadBitmap(int resId){
bm = getBitmap();
ImageView imageView = oa.getPos();
if (cancelPotentialWork(resId, imageView)) {
final BitmapWorkerTask task = getBitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable =
new AsyncDrawable(Resources.getSystem(), bm, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(resId);
}
}
public boolean cancelPotentialWork(int data, ImageView imageView) {
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (bitmapWorkerTask != null) {
final int bitmapData = bitmapWorkerTask.data;
// If bitmapData is not yet set or it differs from the new data
if (bitmapData == 0 || 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;
}
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;
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
ImageView imageView;
objectAdapter oa;
@Override
protected Bitmap doInBackground(Integer... params) {
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
imageView = oa.getPos();
if (isCancelled()) {
bitmap = null;
}
if (imageView != null && bitmap != null) {
final ImageView imageView = oa.getPos();
final BitmapWorkerTask bitmapWorkerTask =
getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
}
我不知道为什么会出现这个问题,但我认为这可能很简单。
提前致谢!
维达尔
答案 0 :(得分:1)
字段数据是私有的,您应该添加一个getter或将字段可访问性更改为public
答案 1 :(得分:0)
您正在尝试引用名为&#34; data&#34;的字段。在BitmapWorkerTask类中。据我所知,该类没有名称为&#34; data&#34;的字段。我看到了一个&#34;数据&#34;而是AsyncDrawable中的字段。