Android 更快的是:只需设置ImageView.setImageBitmap或检查ImageView是否已附加此位图?
我有一个自定义图像缓存,它基本上执行以下操作: 我这样称呼它:Android 更快的是:只需设置ImageView.setImageBitmap或检查ImageView是否已附加此位图?,android,caching,bitmap,android-resources,android-drawable,Android,Caching,Bitmap,Android Resources,Android Drawable,我有一个自定义图像缓存,它基本上执行以下操作: 我这样称呼它: myImageLoader.imageForImageView(R.id.image_I_want_to_assign, myImageView); 使用以下方法: public void imageForImageView(int id, ImageView iv){ Bitmap bitmap = cachedBitmaps.get(id); // Does this Bitmap already exist
myImageLoader.imageForImageView(R.id.image_I_want_to_assign, myImageView);
使用以下方法:
public void imageForImageView(int id, ImageView iv){
Bitmap bitmap = cachedBitmaps.get(id);
// Does this Bitmap already exist in the HashMap?
if(bitmap != null)
// If the given ImageView isn't null
if(iv != null)
// Then assign the Bitmap to the ImageView
iv.setImageBitmap(bitmap);
// If it doesn't exists yet:
else{
// Create it locally using the BitmapFactory
bitmap = BitmapFactory.decodeResource(context.getResources(), id);
// Does the Drawable with this given Resource-ID exist?
// Yes:
if(bitmap != null){
// Put it in the HashMap
cachedBitmaps.put(id, bitmap);
// and assign it to the ImageView
if(iv != null)
iv.setImageBitmap(bitmap);
L.Log(TAG, "New Image added to the list: " + context.getResources().getResourceEntryName(id), LogType.INFO);
}
// No:
else
// Use the default Resource-ID instead
if(iv != null)
iv.setImageBitmap(cachedBitmaps.get(DEFAULT_CHECKBOX));
}
}
我的问题是我是否应该保持:
if(bitmap != null)
if(iv != null)
iv.setImageBitmap(bitmap);
或者检查此ImageView是否尚未附加此位图,如果没有,请添加它:
if(bitmap != null)
if(iv != null && ((BitmapDrawable)iv.getDrawable()).getBitmap() != bitmap)
iv.setImageBitmap(bitmap);
什么更快?使用包含BitmapDrawable强制转换的getDrawable().getBitmap
检查ImageView是否已经具有此位图,或者即使它已经具有与图像相同的位图,也不进行检查就设置它
编辑1
如果有人知道通过某种单元测试(10000次以防止纳秒/微秒的不安全性)检查两个选项之间的时间差的方法,我将不胜感激。或者有某种调试器风格的计时器Eclipse插件,所以我可以设置两个计时器点(开始和结束),它会给我一个它们之间的时间日志?所以你不知道如何测量方法调用之间的时间?@Foxinsocks no,从未使用过它。我想我可以在
if(bitmap!=null)
之后启动计时器,然后在iv.setImageBitmap(bitmap)
之后再次停止计时器,无论是否使用if,尽管我认为它产生的纳秒/微秒都会变化太大。是否有一种方法可以对其进行单元测试,例如,将其调用10.000次,然后查看时间差?你会如何安排时间?是的,你能做到。但这是另一个问题。所以,要么创建新问题,要么编辑这个问题。