简体   繁体   English

android canvas ontouchevent 不工作

[英]android canvas ontouchevent doesn't work

I have a class for my custom view.我的自定义视图有一个 class。 One of methods is其中一种方法是

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

There are 8 regions for my shapes.我的形状有 8 个区域。 So, in the activity class所以,在活动 class

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

but it doesn't work.但它不起作用。 When I click on the last shape toast is called twice.当我点击最后一个形状时,吐司被调用了两次。 but on other shapes it is called some times.... help me ^_^但在其他形状上它有时被称为....帮助我^_^

so this code works fine )所以这段代码工作正常)

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

and

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

React only on one action.只对一个动作做出反应。 Use ACTION_DOWN or ACTION_UP in your onTouch() method, otherwise it will at least be called twice if you hit a region.在你的onTouch()方法中使用ACTION_DOWNACTION_UP ,否则如果你点击一个区域,它至少会被调用两次。

Could you also please specify what on other shapes it called some times exactly means?您能否也请说明on other shapes it called some times确切含义?

Edit:编辑:

You should create the regions not inside your onDraw method.您应该创建不在 onDraw 方法内的区域。 Do it somewhere else (constructor?).在其他地方做(构造函数?)。

A small tipp: if you want to see if your regions are correct, you should draw the rects/regions on the screen at the very last, so that they overlay the playground.一个小技巧:如果你想看看你的区域是否正确,你应该在最后在屏幕上绘制矩形/区域,以便它们覆盖操场。 That should give you a visual feedback if your regions are in the right place and have the right size.如果您的区域位于正确的位置并且具有正确的大小,那应该会给您视觉反馈。 Also alter the color of the regions you are drawing as it might help to see some issues.还要更改您正在绘制的区域的颜色,因为这可能有助于发现一些问题。

If you have done that, could you provide a screenshot for it?如果你这样做了,你能提供一个截图吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM