0
我正在嘗試構建一個按鈕,該按鈕顯示在按鈕的中心,而不是文本,只要點按該按鈕,並且一些背景處理髮生。 我正在做的是有一個自定義按鈕,並將動畫設置爲按鈕左側的可繪製左側,並將onDraw中的動畫設置移動到按鈕中央。但是,在點擊按鈕後的很短時間內,我不知道爲什麼按鈕上有兩個進度條。單擊按鈕時在按鈕上顯示動畫而不是文本
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);
}
}
}
而且,因爲我想這是顯示水龍頭後一小段時間,之前的一切期待:
任何想法,爲什麼發生這種情況?