1

i am creating and android turn based game with Android SDK.

My problem is i don't know how to detect an onclick event of a form like this with a selector, since selector only have oval, rectangle, ring and line form.

image

Any idea?

Solution

According to this post https://stackoverflow.com/a/14516572/2139691 the complete code of a custom view for a concave or convex forms would be this:

public class CustomView extends View {

private final Bitmap bitmap;

public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(bitmap, this.getX(), getY(), null);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    float iX = event.getX();
    float iY = event.getY();

    switch (event.getAction()) {

    case MotionEvent.ACTION_DOWN:
        //Makes sure that X and Y are not less than 0, and no more than the height and width of the image.
        if (iX >= 0 & iY >= 0 & iX < bitmap.getWidth() & iY < bitmap.getHeight()) {
            if (bitmap.getPixel((int) iX, (int) iY) != 0) {
                Log.i("Custom view", "Touched!!!");
            }
        }
        return true;
    }
    return false;

}

}

Hope it helps to others who looks for the tittle "concave or convex views" instead of a "custom view".

Community
  • 1
  • 1
Ricardo
  • 7,921
  • 14
  • 64
  • 111

1 Answers1

2

This post pretty much suggests a couple of solutions to your problem Android Custom Shape Button

Community
  • 1
  • 1
OinkManji
  • 111
  • 1