我希望有一个图标,点击后会被旋转的进度条取代。完成后台中的相应任务后,ProgressBar
应再次替换为图标。
这类似于我们在操作栏中习惯的进度条(例如,如here所述),但我想在Fragment
(对话框)中实现相同的功能,所以setActionView()不可用。
解决这个问题的最佳方法是什么?
答案 0 :(得分:-1)
所以你的下一站是ProgressButton SundeepK(麻省理工学院执照)
ProgressButton 可用于显示简单的旋转Drawable以便为用户提供 加载按钮的效果。一旦用户点击按钮,将显示Drawable,并且必须使用 stopLoadingAnimation()方法手动关闭。
ProgressButton类:
public class ProgressButton extends ImageButton {
private boolean _shouldDisplayLoadingAnimation = false;
private Drawable _loadingAnimation;
private TextPaint _textPaint;
private Rect _textBounds;
private String _defaultText;
public ProgressButton(Context context_, AttributeSet attrs_, int defStyle_) {
super(context_, attrs_, defStyle_);
final TypedArray a = context_.obtainStyledAttributes(attrs_, R.styleable.ProgressButton,
R.attr.progressButtonStyle, R.style.ProgressButton_attrs);
this.setBackgroundColor(a.getInteger(R.styleable.ProgressButton_defaultBackgroundColor, Color.WHITE));
_loadingAnimation = getDrawable();
_loadingAnimation.setAlpha(0);
_defaultText = a.getString(R.styleable.ProgressButton_defaultText);
_textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
_textPaint.density = getResources().getDisplayMetrics().density;
_textPaint.setColor(a.getInteger(R.styleable.ProgressButton_defaultFontColor, Color.BLACK));
_textPaint.setTextAlign(Align.CENTER);
_textPaint.setTextSize(a.getInteger(R.styleable.ProgressButton_defaultFontSize, 40));
_textPaint.setFakeBoldText(true);
_textBounds = new Rect();
a.recycle();
}
public ProgressButton(Context context_, AttributeSet attrs_) {
this(context_, attrs_, 0);
}
public ProgressButton(Context context_) {
this(context_, null);
}
@Override
public boolean performClick() {
boolean isClicked = super.performClick();
if (isClicked) {
_shouldDisplayLoadingAnimation = true;
this.invalidate();
}
return isClicked;
};
public void startLoadingAnimation() {
_shouldDisplayLoadingAnimation = true;
this.invalidate();
}
public void stopLoadingAnimation() {
_shouldDisplayLoadingAnimation = false;
this.invalidate();
}
@Override
protected void onDraw(Canvas canvas_) {
if (_shouldDisplayLoadingAnimation) {
shouldShowAnimation(true);
} else {
_textPaint.getTextBounds(_defaultText, 0, _defaultText.length(), _textBounds);
canvas_.drawText( _defaultText , getWidth()/2, (getHeight()/2)+((_textBounds.bottom-_textBounds.top)/2) , _textPaint);
shouldShowAnimation(false);
_loadingAnimation.setVisible(false, false);
}
super.onDraw(canvas_);
}
private void shouldShowAnimation(boolean shouldShow_) {
if (_loadingAnimation instanceof Animatable) {
if (shouldShow_) {
_loadingAnimation.setAlpha(255);
((Animatable) _loadingAnimation).start();
} else {
_loadingAnimation.setAlpha(0);
((Animatable) _loadingAnimation).stop();
}
}
}
}
在布局中定义ProgressButton:
<com.sun.progressbutton.ProgressButton
android:id="@+id/progressView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="10dp"
android:src="@drawable/progress_view"
/>