I have not tested this, but I think it will work. I am sure you will have to configure it and make corrections.
Create a copy of the background image in which the three buttons are three different colors without aliasing and all the other pixels are pure black. It must be PNG, so it will not have JPG compression artifacts. This will be your mask. You can just use pure red, green and blue, since you only have three buttons. This could probably be a lower resolution than the original image if the aspect ratio is the same.
Subclass ImageView and use your subclass to paint the background. Pass your subclass the version of ImageView into your mask image as a bitmap.
class TouchMaskImageView extends ImageView { ...constructors public void setTouchMask(Bitmap mask){ this.mMask = mask; } public interface OnTouchedListener{ public void onTouched(MotionEvent event, int colorTouched); public void onTouchCanceled(); } public void setOnTouchedListener(OnTouchedListener listener){ this.mOnTouchedListener = listener; } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); if (mOnTouchedListener != null && mMask != null ) { if (x >=0 && x < (float)getWidth() && y >=0 && y < (float)getHeight()) {
Now you can create an OnTouchedListener that handles touch logic similar to a button. He will have to track the status. Here is an example, but I'm not sure if it breaks when there are a few touches. You may also need to set the action mask for the first finger and use getActionMasked () instead of getAction ().
public void onTouched(MotionEvent event, int colorTouched){ switch(mState){ case STATE_WAITING: if (event.getAction()==MotionEvent.ACTION_DOWN){ switch (colorTouched){ case 0xffff0000: //red mTouchMaskImageView.setImageResource(R.drawable.redButtonDown); mState = STATE_LATCHED_RED; break; case 0xff00ff00: //green mTouchMaskImageView.setImageResource(R.drawable.greenButtonDown); mState = STATE_LATCHED_GREEN; break; case 0xff0000ff: //blue mTouchMaskImageView.setImageResource(R.drawable.blueButtonDown); mState = STATE_LATCHED_BLUE; break; } } break; case STATE_LATCHED_RED: switch (event.getAction()){ case (MotionEvent.ACTION_MOVE): if (colorTouched = 0xffff0000) mTouchMaskImageView.setImageResource(R.drawable.redButtonDown); else mTouchMaskImageView.setImageResource(R.drawable.defaultImage); break; case (MotionEvent.ACTION_UP) if (colorTouched = 0xffff0000) performRedButtonAction(); mTouchMaskImageView.setImageResource(R.drawable.defaultImage); mState = STATE_WAITING; break; } break; case etc. for other two latched colors... break; } } public void onTouchCanceled(){ mTouchMaskImageView.setImageResource(R.drawable.defaultImage); mState = STATE_WAITING; }
This is somewhat crude, because you need four different copies of your image to represent the four possible states. If you want to take the time to save some memory and make it work faster, you can try to customize the layout with separate images for the three buttons superimposed in the correct positions, and configure them to change the image. But that would be very difficult, given that you would have to ideally line it up for any screen size.
Tenfour04
source share