我正试图在我的应用程序中创建一个画廊遵循谷歌网站的一般准则。我让画廊用 AsyncTask
(代码如下)。
然而,有两个问题:
1) 每次我离开并返回画廊活动时,图像会一个接一个地重新加载。美术馆是在一个带有 setRetainInstance(true)
.
2) 图像加载速度不是很快,有时可能会延迟(例如,如果我在 ViewPager
在局部视图中太快)。
为了解决这个问题,我想我必须实施 LruCache
以及 DiskCache
教程的各个部分,请参见此处。但我不知道如何做到这一点,同时维护我已经写了下面的代码。谷歌的教程似乎有重叠——例如,方法 loadBitmap
在缓存教程中是不同的,我不知道如何将其与下面的内容合并。
注意:这些图像保存到内部存储-我不下载它们。
有人能给我一些建议/例子来说明如何实施 LruCache
以及 DiskCache
输入下面的代码?我对android的这些部分非常陌生,它可能会让人困惑。谢谢您! GalleryAdapter.java
:
@Override
public View getView(int position, View view, ViewGroup viewGroup)
{
View v = view;
ImageView imageView;
if (v == null)
{
v = inflater.inflate(R.layout.gallery_row, viewGroup, false);
v.setTag(R.id.image, v.findViewById(R.id.image));
}
imageView = (ImageView) v.getTag(R.id.image);
loadBitmap(position, imageView);
return v;
}
public void loadBitmap(int data, ImageView imageView)
{
if (cancelPotentialWork(data, imageView))
{
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
final AsyncDrawable asyncDrawable = new AsyncDrawable(mContext.getResources(), mPlaceHolderBitmap, task);
imageView.setImageDrawable(asyncDrawable);
task.execute(data);
}
}
static class AsyncDrawable extends BitmapDrawable
{
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask)
{
super(res, bitmap);
bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(bitmapWorkerTask);
}
public BitmapWorkerTask getBitmapWorkerTask()
{
return bitmapWorkerTaskReference.get();
}
}
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;
}
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>
{
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView)
{
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(Integer... params)
{
data = params[0];
return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(mPaths.get(data)), 100, 100, true);
}
@Override
protected void onPostExecute(Bitmap bitmap)
{
if (isCancelled())
{
bitmap = null;
}
if (imageViewReference != null && bitmap != null)
{
final ImageView imageView = imageViewReference.get();
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask && imageView != null)
{
imageView.setImageBitmap(bitmap);
}
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!