我有一个自定义视图的课程。其中一种方法是
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint ();
Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench);
canvas.drawColor(Color .BLACK);
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int left = canvas.getWidth()/2 - wrench.getWidth()*2 + j*wrench.getWidth();
int top = 0 + i*wrench.getHeight();
canvas.drawBitmap(wrench, left, top, null);
regions = new ArrayList<Region>();
Region reg = new Region(left, top, left + wrench.getWidth(), top + wrench.getHeight());
regions.add(reg);
// int right = left + wrench.getWidth();
// int bottom = top + wrench.getHeight();
// Log.d("REGION", left + "," + top + "," + right + "," + bottom);
}
}
}
我的形状有8个区域。所以,在活动类
中 playField = (PlayGameView) findViewById(R.id.play_field_surface_view);
playField.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// if(playField.getRegions().contains(210, 10)) {
ArrayList<Region> regions = playField.getRegions();
for(Region region: regions ) {
boolean condition = region.contains( (int)event.getX(), (int)event.getY());
if(condition) {
Toast.makeText(PlayGameActivity.this, (int)event.getX() + " " + (int)event.getY(), 2000).show();
}
}
return true;
}
});
但它不起作用!当我点击最后一个形状吐司被叫两次!但在其他形状上它被称为有时....帮助我^ _ ^
所以这段代码工作正常)
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint ();
Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench);
canvas.drawColor(Color .BLACK);
shapeWidth = wrench.getWidth();
shapeHeight = wrench.getHeight();
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
int left = canvas.getWidth()/2 - wrench.getWidth()*2 + j*wrench.getWidth();
int top = i*wrench.getHeight();
canvas.drawBitmap(wrench, left, top, null);
shapeLeft[j] = left;
shapeTop[i] = top;
}
}
}
public ArrayList<Region> getRegions() {
regions = new ArrayList<Region>();
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
Region reg = new Region(shapeLeft[j], shapeTop[i], shapeLeft[j] + shapeWidth, shapeTop[i] + shapeHeight);
regions.add(reg);
// int right = left + wrench.getWidth();
// int bottom = top + wrench.getHeight();
// Log.d("REGION", left + "," + top + "," + right + "," + bottom);
}
}
return regions;
}
和
playField = (PlayGameView) findViewById(R.id.play_field_surface_view);
playField.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// if(playField.getRegions().contains(210, 10)) {
ArrayList<Region> regions = playField.getRegions();
for(Region region: regions ) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
boolean condition = region.contains( (int)event.getX(), (int)event.getY());
if(condition) {
Toast.makeText(PlayGameActivity.this, (int)event.getX() + " " + (int)event.getY(), 2000).show();
}
}
}
return true;
}
});
答案 0 :(得分:2)
仅对一个动作做出反应。在ACTION_DOWN
方法中使用ACTION_UP
或onTouch()
,否则如果您遇到某个地区,则至少会被调用两次。
您还能指出on other shapes it called some times
究竟意味着什么吗?
编辑:
您应该创建不在onDraw方法中的区域。在其他地方做(构造函数?)。
一个小小的tipp:如果你想看看你的地区是否正确,你应该在最后画出屏幕上的rects / regions,以便它们覆盖游乐场。如果您的地区位于正确的地方且尺寸合适,那么应该会给您一个视觉反馈。同时改变您正在绘制的区域的颜色,因为它可能有助于查看某些问题。
如果你这样做了,你能为它提供截图吗?