【发布时间】:2013-07-28 16:01:56
【问题描述】:
我正在使用 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;
}
}
希望它对寻找标题“凹面或凸面视图”而不是“自定义视图”的其他人有所帮助。
【问题讨论】:
标签: android