Overlay a bitmap on another
As I’ve been alluding to in my Twitter feed, I’m on the cusp of deploying a new version of MediaDroid, huge re-write to image gallery, but hopefully I’ll get a post about that out later. Anyway, the previous way I was marking the images to be included in the albums was to have a combo view with a check box and a thumbnail of the image. Although this works, it takes up a lot of screen real estate and isn’t terribly efficient. The next version of the image gallery I wrote extended the Android ImageView class and although this solution isn’t much more efficient (the solution for that is in my newest version), it certainly increases the number of images viewed at a time.
Because I’m revising this screen again, the ImageView implementation is being re-factored out, but I didn’t want to loose the knowledge to the abyss of my Git repository so I’m posting it to share and for my own benefit for future projects.
The OverlayImage view contains a setter allowing you to set the image displayed in the view. There are other methods to set and retrieve the status of the “checked” image. When an image is clicked, another image (in my case the checked.png bitmap in the drawable resources folder; and yes, I created my own “checks”…with crayons…more on that in a future post) is drawn on top during the onDraw method. Depending on your implementation, you may have to call invalidate() on the view to force the redraw.
public class OverlayImageView extends ImageView
{
private boolean _onflag;
private Uri _imgUri;
public OverlayImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
_onflag = true;
}
public void setImgUri( Uri imgUri )
{
_imgUri = imgUri;
}
public void setImage( Uri imgUri, Bitmap bm )
{
if ( imgUri.equals( _imgUri ) )
{
super.setImageBitmap(bm);
}
}
public void setCheckedFlag( boolean checked )
{
_onflag = checked;
OverlayImageView.this.invalidate();
}
public boolean isChecked()
{
return _onflag;
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if ( _onflag )
{
Bitmap b = BitmapFactory.decodeResource( this.getResources(), R.drawable.checked );
canvas.drawBitmap(b, 10.0f, 10.0f, null);
}
}
}
And for those who are interested, I’m hoping on having the newest version of MediaDroid on the Android market early next week. Hope you get a chance to check it out and PLEASE, let me know what you think, always looking for feedback!!


February 5th, 2010 at 8:19 pm
[…] for that is in my newest version), it certainly increases the number of images viewed at a time. (more…) All, […]
June 28th, 2010 at 12:22 pm
Are you using this from code only or can you use that in the xml layout somehow as well?
June 28th, 2010 at 12:34 pm
Manfred: As far as I know, this is from code and not a layout, but I can’t say I’ve experimented much with it. I know that you can define button states from a resource file, but for the life of me I can’t find the example. This may be an option…but you still need someway of capturing the state of the image to save for later.