5

There are two adjacent graphic buttons. If I use the method of calculation of the coordinates touch TouchEvent, I have to create a large array of coordinates. Maybe there is another method?

ImageButton

user
  • 86,916
  • 18
  • 197
  • 190
Roman
  • 1,045
  • 1
  • 12
  • 33

1 Answers1

7

Probably not the best solution but you could overlap two buttons each representing a part of the image(the black or white) and make the rest of the Button's image transparent(the images will have the same dimensions for both buttons). You'll also need to extend the Button class to override how the onTouchEvent method handles the touch events, depending on the point where the touch happened being a transparent point or an "image" point. I've made a small sample project obtaining a Button with two parts like below(ignore my design skills):

enter image description here

You can find the project here, it's pretty self explanatory. See if it helps you.

user
  • 86,916
  • 18
  • 197
  • 190
  • 1
    Thanks for your solution. It helped me. However, I've found a problem with your sample. If the drawable is not in "wrap_content" (for example, fill_parent), your project will crash. I have fixed it by creating the bitmap like that : Bitmap b = Bitmap.createScaledBitmap( ((BitmapDrawable) bkg.getCurrent()).getBitmap(), getWidth(), getHeight(), true ); – Sébastien BATEZAT Feb 28 '13 at 18:55
  • 1
    @Sebastien You are right, my code didn't take in consideration the view's size changing(or being bigger that the Bitmap itself). I've corrected the sample and I'm scaling the `Bitmap` in the `onSizeChanged` callback so it always has the correct size. – user Mar 01 '13 at 13:26
  • Your solution is better that mine (to do it in onSizeChanged). Thanks again :) However, I have improved it again, before creating the bitmap, I have add this code : // Bitmap take a lot of place in memory, free memory as soon as possible if (mBitmap != null){ mBitmap.recycle(); } – Sébastien BATEZAT Jun 07 '13 at 19:06