0

我有一个自定义视图的类。一种方法是

    @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;
        }
    });
4

1 回答 1

2

只对一个动作做出反应。在你的方法中使用ACTION_DOWNor ,否则如果你击中一个区域,它至少会被调用两次。ACTION_UPonTouch()

您能否具体说明一下具体是on other shapes it called some times什么意思?

编辑:

您应该创建不在 onDraw 方法内的区域。在其他地方做(构造函数?)。

一个小提示:如果您想查看您的区域是否正确,您应该最后在屏幕上绘制矩形/区域,以便它们覆盖操场。如果您的区域位于正确的位置并且大小合适,那应该会给您一个视觉反馈。还要更改您正在绘制的区域的颜色,因为它可能有助于查看一些问题。

如果你这样做了,你能提供一个截图吗?

于 2012-04-04T08:37:41.023 回答