Java 如何将 onclick 侦听器设置为画布?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20613156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:07:51  来源:igfitidea点击:

How to set an onclick listener to a canvas?

javaandroidandroid-canvasonclicklistener

提问by

I have some rectangles drawn on Canvas. And now I need for each rectangle an onclickListener. But because I'm very new to android and specially Canvas, I need some help. Anyway is it possible to add a listener?

我在 上绘制了一些矩形Canvas。现在我需要为每个矩形一个onclickListener. 但是因为我对 android 很陌生,特别是Canvas,我需要一些帮助。无论如何可以添加一个监听器吗?

It looks like this:

它看起来像这样:

enter image description here

在此处输入图片说明

And this is my code for it:

这是我的代码:

RelativeLayout ll = (RelativeLayout) findViewById(R.id.rect);
        Paint paint = new Paint();
        Paint paintForSize = new Paint();
        paintForSize.setColor(Color.parseColor("#FFFFFF")); 
        paintForSize.setTextSize(45);
        Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bg);

        int left = 0; // initial start position of rectangles (50 pixels from left)
        int leftText =70;
        int topText = 93;
        int top = 50; // 50 pixels from the top
        int width = 60;
        int height = 60;

        for(int x = 0; x < 4; x++) { // draw 4 rectangles
            if(x == 0){
            paint.setColor(Color.parseColor("#FF7979"));
            canvas.drawText("Rec 1", leftText, topText, paintForSize);
            }
            if(x == 1){
            paint.setColor(Color.parseColor("#FFD060"));
            canvas.drawText("Rec 2", leftText, topText, paintForSize);
            }
            if(x == 2){
            paint.setColor(Color.parseColor("#B6DB49"));
            canvas.drawText("Rec 3", leftText, topText, paintForSize);
            }
            if(x == 3){
            paint.setColor(Color.parseColor("#CB97E5"));
            canvas.drawText("Rec 4", leftText, topText, paintForSize);
            }

            canvas.drawRect(left, top, left+width, top+height, paint);

            top = (top + height + 10); // set new left co-ordinate + 10 pixel gap

            topText = (topText + height + 10);
        }



        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        ImageView iV = new ImageView(this);
        iV.setImageBitmap(bg);

        ll.addView(iV,params);

Can anybody guide me through this, how should I do it?

任何人都可以指导我完成这个,我该怎么做?

采纳答案by Husman

Might I suggest rethinking how this is going to work. If these rectangles are going to be buttons of some sort, perhaps your better off drawing buttons in your user interface, or using images instead of the rectangles. (Sorry didnt realise this is what you were going to do in your previous question)

我是否建议重新考虑这将如何运作。如果这些矩形将成为某种按钮,也许您最好在用户界面中绘制按钮,或者使用图像而不是矩形。(对不起,没有意识到这是你在上一个问题中要做的)

Have a look at: Difference between a clickable ImageView and ImageButton

看一看:Difference between a clickable ImageView and ImageButton

You can set an eventlistener on your imageview, as mentioned in another answer. See here: Image in Canvas with touch events

您可以在 imageview 上设置事件侦听器,如另一个答案中所述。请参阅此处:带有触摸事件的画布中的图像

回答by Naddy

Canvasis not a View. You cannot add listeners to them. You could create a custom View implementation. Whatever you do with the Canvas, implement that in onDraw(), which receives a Canvasas a parameter. Then add your view to a layout and give it whatever listeners you want. You may as well look into SurfaceViewfor drawing, which is a View.

Canvas不是View. 您不能向它们添加侦听器。您可以创建自定义视图实现。无论你用 做什么,在Canvas中实现它onDraw(),它接收 aCanvas作为参数。然后将您的视图添加到布局中,并为其提供您想要的任何侦听器。您不妨研究一下SurfaceView绘图,这是一个View.

Thismight help you.

可能对你有帮助。