I've got a custom Image-cache which basically does the following:
I call it like this:
myImageLoader.imageForImageView(R.id.image_I_want_to_assign, myImageView);
With the method:
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));
}
}
My question is whether I should just keep:
if(bitmap != null)
if(iv != null)
iv.setImageBitmap(bitmap);
Or check if this ImageView doesn't already have this Bitmap attached, and if not add it:
if(bitmap != null)
if(iv != null && ((BitmapDrawable)iv.getDrawable()).getBitmap() != bitmap)
iv.setImageBitmap(bitmap);
What is faster? Checking if the ImageView already had this Bitmap by using getDrawable().getBitmap
including the BitmapDrawable cast, or just set it without checking even if it already had this same Bitmap as Image?
EDIT 1
If someone knows a way to check the time difference between the two options with some kind of UnitTest (10.000 times to prevent insecurities in the nano/microseconds) I would appreciate it. Or is there some kind of Debugger-style timer Eclipse-Plugin, so I can just set two timer-points (start and end) and it will give me a log of the time in between them?