Java 在developer.android.com上缓存位图错误
我刚才正在阅读从url缓存位图的教程 参考代码Java 在developer.android.com上缓存位图错误,java,android,Java,Android,我刚才正在阅读从url缓存位图的教程 参考代码 private LruCache<String, Bitmap> mMemoryCache; @Override protected void onCreate(Bundle savedInstanceState) { ... // Get memory class of this device, exceeding this amount will throw an
private LruCache<String, Bitmap> mMemoryCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager) context.getSystemService(
Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = 1024 * 1024 * memClass / 8;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than number of items.
return bitmap.getByteCount();
}
};
...
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
public void loadBitmap(int resId, ImageView imageView) {
final String imageKey = String.valueOf(resId);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
} else {
mImageView.setImageResource(R.drawable.image_placeholder);
BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
task.execute(resId);
}
}
现在我的问题是,在BitmapWorkerTask类中,方法如何添加BitMaptomeryCache(String.valueOf(params[0]),位图)调用strong>时,可以引用Activty中定义的方法或其他类中定义的方法BitmapWorkerTask可能是activity中的一个内部类,因此它可以访问已定义的方法 对于仍然无法使用示例代码的人 为了使代码正常工作,应该将BitmapWorkerTask类设置为内部类,然后更改其两个静态函数
public static int calculateInSampleSize()
public static Bitmap decodeSampledBitmapFromResource()
对于普通(非静态)方法现在我有了开发者。android他们可能喜欢内部类:)虽然这是一个很好的教程,缓存BIMAP的好方法和短方法,通过它你可以轻松实现惰性列表加载。我读过许多其他教程,它们很长,很浪费时间,我已经在代码中实现了这一点,并且在将BitmapWorkerTask声明为内部类时会出现语义错误。Java编译器抱怨内部类中的静态方法。如果我将内部类设为静态,那么AddBitMaptomeryCache()再次无法访问。“calculateInSampleSize方法不能声明为静态;静态方法只能在静态或顶级类型中声明”
public static int calculateInSampleSize()
public static Bitmap decodeSampledBitmapFromResource()