gpt4 book ai didi

android: onDraw 不断被调用

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:03 26 4
gpt4 key购买 nike

我的自定义按钮有问题。 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);

任何想法可能是错误的?

最佳答案

改变drawableStateChanged()方法:

  1. 不要调用invalidate
  2. 调用super.drawableStateChanged()

关于android: onDraw 不断被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7814502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com