Android - Image on the button Pressed event - Apply image overlay on everything

My question is related to applying image overlay to all pressed buttons. So, for example, in Android 4.0 and later with the installation of a hologram, when you press any button, it has a light blue overlay. In an earlier version of Android, it is orange. I know how to configure a button to change the image for the pressed, focused and default XML using the selector and element tags, but this will require me to make another image of my button, but with a blue overlay, which means that I know there are 2 pictures for each button. There will be many buttons in my application, and I would like the application file size to be as small as possible.

So, the question is, is there a way to make only 1 image (white 25% opaque image) apply as an overlay for all button presses, and also keep the original background of the button that I set is there?

+4
source share
1 answer

Upload image. Then draw an alpha color on it.

Then use the modified image to set the Image button.

Bitmap img = BitmapFactory.decodeFile(pathName); // or decodeResource etc.... Canvas canvas = new Canvas(img); Paint alphaPaint = new Paint(); // Color to apply. alphaPaint.setColor(Color.BLUE); alphaPaint.setAlpha(20); // Draw rectangle over your image using the alpha colored paint. canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), alphaPaint); myButton.setImageBitmap(img ); 
+2
source

All Articles