1

我正在尝试在 Canvas 上绘制一个形状并获取有关用户在该 Canvas 上执行的点击的信息。

我创建了一个扩展 Button 类的类。

  class CustomDrawableButton extends Button {
    private final ShapeDrawable mDrawable;
    int x = 50;
    int y = 50;
    int width = 30;
    int height = 30;

    public CustomDrawableButton (Context context) {
       super(context);
       mDrawable = new ShapeDrawable (new OvalShape ());
       mDrawable.getPaint().setColor(Color.GREEN);
       mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
      mDrawable.draw(canvas);
    }
  }

然后,在同样扩展 View 的类中,我添加了实例化和侦听器:

  CustomDrawableButton mCustomDrawableButton = new CustomDrawableButton(getBaseContext());

  layout.addView(mCustomDrawableButton);

  mCustomDrawableButton.draw(canvas);

  mCustomDrawableButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
      System.out.println("Yey clicked");
     Toast.makeText(view.getContext(), "Yey", Toast.LENGTH_LONG).show();
    }});

执行此操作时,它无法检测到对图像/按钮的点击。我已经读过 ShapeDrawable 对象不能是“可点击的”,但我期待它能够工作,因为我正在扩展按钮(这是一个视图并且是“可点击的”)。

这可能吗?如果不是这种方式,您能否指导我以某种方式获取有关在 Canvas 或 View 上按下的屏幕坐标的信息?

提前致谢。

4

1 回答 1

0

After some searching here's a tutorial that shows how to do it using by getting the touched screen position.

http://marakana.com/tutorials/android/2d-graphics-example.html

Still don't know how to do it by automatically binding the touched event to a button. If anyone knows how to do it, please say so.

Thanks.

于 2010-08-19T20:56:11.947 回答