单击时,在按钮上显示动画而不是文本

时间:2013-02-06 11:42:04

标签: android-layout button

我正在尝试构建一个带有进度条的按钮,该按钮显示在按钮的中心,而不是文本,只要按下按钮,并且进行一些后台处理。 我正在做的是有一个自定义按钮,并将动画设置为左侧的可绘制按钮,并在onDraw中移动按钮中心的动画。但是,在点击按钮后的短时间内,我不知道为什么按钮上有2个进度条。

public class ButtonWithProgressBar extends Button {
protected int progressId = R.drawable.progress_bar;
protected Drawable progress;
protected CharSequence buttonText;

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if (Strings.notEmpty(text)) {
        buttonText = text;
    }
}

public void startSpinning()
{
    if(isSpinning()){
        return;
    }

    progress = getContext().getResources().getDrawable(progressId);
    final Drawable[] old = getCompoundDrawables();
    setCompoundDrawablesWithIntrinsicBounds(old[0], old[1], progress, old[3]);
    setEnabled(false);

    if(progress instanceof Animatable){
        ((Animatable)progress).start();
    }
    setText("");

}

public void stopSpinning()
{
    if(!isSpinning()){
        return;
    }

    if(progress instanceof Animatable){
        ((Animatable)progress).stop();
    }
    setEnabled(true);
    setText(buttonText);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (progress != null) {
        final int drawableHeight = progress.getIntrinsicHeight();
        final int drawableWidth = progress.getIntrinsicWidth();
        final int drawableTop = (getHeight() - drawableHeight) / 2;
        final int drawableLeft = (getWidth() - drawableWidth) / 2;
        progress.setBounds(drawableLeft, drawableTop, drawableLeft + drawableWidth, drawableTop + drawableHeight);

        progress.draw(canvas);
    }
}

}

点击后显示一段时间,然后按照我的要求查看: enter image description here

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

发现问题。已更新的代码为工作版本。