I am trying to implement four custom buttons, as you can see in the following figure:

What I have done so far: I took 4 different pictures - each of them was visible only in one color (see above). The other part of the image is transparent. This leads to the fact that I have four images of the same size.
Now I used the relative layout, where all my 4 images are added to the images in the same position. Because of the transparency, I see the picture I want.
For my ImageViews, I implemented onTouchListener with the following content:
private class ImageOnTouchListener implements View.OnTouchListener { private int categoryId; public ImageOnTouchListener(int categoryId) { this.categoryId = categoryId; } @Override public boolean onTouch(View v, MotionEvent event) { Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache()); int x = (int) event.getX(); int y = (int) event.getY(); boolean isInsideBitmap = x < bmp.getWidth() && y < bmp.getHeight() && x >= 0 && y >= 0; boolean isActionUp = event.getAction() == MotionEvent.ACTION_UP; if (isInsideBitmap) { int color = bmp.getPixel(x, y); bmp.recycle(); if (color == Color.TRANSPARENT){ return false; } else { if (isActionUp) { buttonClick(); } } }else{ bmp.recycle(); } return true; } }
This approach works, but it consumes a lot of memory since I always create a bitmap when I move my finger. I'm not quite sure if this is the best way to implement this. Is there something I can do differently that can lead to a more efficient way?
source share