For an Android app I'm building, I made a custom ImageView class which needs to show a tick over the image indicating this image has been selected. I loaded the tick image from a drawable resource and displayed it on top of the image, and it works fine. But what I want to do is that, when the tick is visible (that is, when the image is selected), the image to become darker so that you can see the tick clearly. How can I do this?
My code currently is as follows:
public class TickedImageView extends ImageView {
private boolean selected = true;
private Bitmap tickBmp;
private Paint paint = new Paint();
public TickedImageView(Context context) {
super(context);
tickBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_done_white_48dp);
}
public TickedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
tickBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_done_white_48dp);
}
public void setSelected(boolean selected) {
this.selected = selected;
invalidate();
}
public boolean isSelected() {
return selected;
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
if(selected) {
int margin = 15;
int x = (canvas.getWidth() / 2) - (tickBmp.getWidth() / 2) - margin;
int y = (canvas.getHeight() / 2) - (tickBmp.getHeight() / 2) - margin;
canvas.drawBitmap(tickBmp, x, y, paint);
}
}
}
Thank you.