机器人:OnDraw的不断呼吁 [英] android: onDraw is called constantly
本文介绍了机器人:OnDraw的不断呼吁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义按钮的一个问题。的OnDraw()被一次又一次地叫,我想不通这是为什么。
I have a problem with a custom button. onDraw() gets called again and again, and I can't figure out why.
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);
任何想法可能是错误的?
Any ideas what might be wrong?
推荐答案
抱歉,哑失败:
this.getDefaultBitmap()
有一个 setBackgroundColor(00000000)
;
调用它而重新触发OnDraw中。
this.getDefaultBitmap()
had a setBackgroundColor(0x00000000)
;
call in it which retriggered the onDraw.
的OnDraw()
- > getDefaultBitmap()
- > setBackgroundColor()
- > 的OnDraw
onDraw()
-> getDefaultBitmap()
-> setBackgroundColor()
-> onDraw
这篇关于机器人:OnDraw的不断呼吁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文