我正在使用 Android SDK 创建和基于 Android 回合的游戏。
我的问题是我不知道如何使用选择器检测这样的表单的 onclick 事件,因为选择器只有椭圆形、矩形、环形和线形。
任何想法?
解决方案
根据这篇文章https://stackoverflow.com/a/14516572/2139691,凹形或凸形的自定义视图的完整代码如下:
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;
}
}