私はカスタムボタンに問題があります。 onDraw()は何度も何度も呼び出されています。理由を理解できません。android:onDrawが常に呼び出されます
public class CircleButton extends Button {
static final int StateDefault = 0;
static final int StateFocused = 1;
static final int StatePressed = 2;
@Override
protected void onDraw(Canvas canvas) {
Log.v("Button","onDraw()");
switch (mState) {
case StateDefault:
canvas.drawBitmap(this.getDefaultBitmap(), 0, 0, null);
break;
case StateFocused:
canvas.drawBitmap(this.getFocusedBitmap(), 0, 0, null);
break;
case StatePressed:
/*this.setWidth(3*radius);
this.setHeight(3*radius);*/
canvas.drawBitmap(this.getFocusedBitmap(), 0, 0, null);
break;
}
super.onDraw(canvas);
}
@Override
protected void drawableStateChanged() {
Log.v("Button","drawableStateChanged()");
if (isPressed()) {
mState = StatePressed;
} else if (hasFocus()) {
mState = StateFocused;
} else {
mState = StateDefault;
}
// force the redraw of the Image
// onDraw will be called!
invalidate();
}
...
}
ボタンは、そのように使用されます。間違っているかもしれないものを
RelativeLayout buttonLayout = (RelativeLayout) this.findViewById(R.id.top_layout);
CircleButton shootButton = new CircleButton(this);
RelativeLayout.LayoutParams relativeParams1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
relativeParams1.addRule(RelativeLayout.CENTER_VERTICAL);
relativeParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeParams1.setMargins(0, 0, -20, 0);
buttonLayout.addView(shootButton, relativeParams1);
任意のアイデア?
は少し遅れたかもしれませんが、この呼び出しの代わりに行ったことを覚えていますか?再描画を強制しないでonDrawの背景色を設定するには? – edthethird
@edthethird onDrawメソッド内からcanvas.drawRectを呼び出します。 –
ああ、ありがとう、それは同様に動作します。 – edthethird