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上按下的屏幕座標的信息嗎?
在此先感謝。