2010-08-17 64 views
1

我试图在画布上绘制形状,并获取有关用户在该画布上执行的点击的信息。如何使用ShapeDrawable创建可点击的按钮?

我创建了一个扩展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); 
    } 
    } 

然后,在其中还扩展视图类,我添加实例化与收听:

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对象不能“可点击”的,但我期待这个工作,因为我扩展了Button(这是一个View并且是“可点击的”)。

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

在此先感谢。

回答