如何创建Circle ProgressDrawable以在壁画中使用?

时间:2016-03-29 08:58:31

标签: android fresco

我是logo库的新手,在将图片从Fresco加载到Progressbar时尝试显示圈uri。现在我使用默认进度条如下:

DraweeView

但是, GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources()); GenericDraweeHierarchy hierarchy = builder.setFadeDuration(fadeInTime).build(); hierarchy.setProgressBarImage(new ProgressBarDrawable()); thumbnailImageView.setHierarchy(hierarchy); ProgressBar?反正有没有把它改成圈?

1 个答案:

答案 0 :(得分:5)

经过大量时间与Google搜索,但我无法找到任何结果。我决定写自己的CircleProgressDrawable。我只想分享给那些不想浪费时间的人。

public class CircleProgressDrawable extends ProgressBarDrawable {

private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mBackgroundColor = 0x80000000;
private int mColor = 0x800080FF;
private int mBarWidth = 20;
private int mLevel = 0;
private boolean mHideWhenZero = false;
private int radius = 50;

public CircleProgressDrawable() {
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeWidth(10f);
}

public void setRadius(int radius) {
    this.radius = radius;
}

/**
 * Sets the progress bar color.
 */
public void setColor(int color) {
    if (mColor != color) {
        mColor = color;
        invalidateSelf();
    }
}

/**
 * Gets the progress bar color.
 */
public int getColor() {
    return mColor;
}

/**
 * Sets the progress bar background color.
 */
public void setBackgroundColor(int backgroundColor) {
    if (mBackgroundColor != backgroundColor) {
        mBackgroundColor = backgroundColor;
        invalidateSelf();
    }
}

/**
 * Gets the progress bar background color.
 */
public int getBackgroundColor() {
    return mBackgroundColor;
}


/**
 * Sets the progress bar width.
 */
public void setBarWidth(int barWidth) {
    if (mBarWidth != barWidth) {
        mBarWidth = barWidth;
        invalidateSelf();
    }
}

/**
 * Gets the progress bar width.
 */
public int getBarWidth() {
    return mBarWidth;
}

/**
 * Sets whether the progress bar should be hidden when the progress is 0.
 */
public void setHideWhenZero(boolean hideWhenZero) {
    mHideWhenZero = hideWhenZero;
}

/**
 * Gets whether the progress bar should be hidden when the progress is 0.
 */
public boolean getHideWhenZero() {
    return mHideWhenZero;
}

@Override
protected boolean onLevelChange(int level) {
    mLevel = level;
    invalidateSelf();
    return true;
}

@Override
public void setAlpha(int alpha) {
    mPaint.setAlpha(alpha);
}

@Override
public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
}

@Override
public int getOpacity() {
    return DrawableUtils.getOpacityFromColor(mPaint.getColor());
}

@Override
public void draw(Canvas canvas) {
    if (mHideWhenZero && mLevel == 0) {
        return;
    }
    drawCircle(canvas, mBackgroundColor);
    drawArc(canvas, mLevel, mColor);
}

private final int MAX_LEVEL = 10000;

private void drawArc(Canvas canvas, int level, int color) {
    mPaint.setColor(color);

    Rect bounds = getBounds();
    // find center point
    int xpos = bounds.left + bounds.width() / 2;
    int ypos = bounds.bottom - bounds.height() / 2;
    RectF rectF = new RectF(xpos - radius, ypos - radius, xpos + radius, ypos + radius);
    float degree = (float) level / (float) MAX_LEVEL * 360;
    canvas.drawArc(rectF, 270, degree, false, mPaint);
    LogUtils.e("level: " + level + ", degree: " + degree);
}

private void drawCircle(Canvas canvas, int color) {
    mPaint.setColor(color);
    Rect bounds = getBounds();
    int xpos = bounds.left + bounds.width() / 2;
    int ypos = bounds.bottom - bounds.height() / 2;
    canvas.drawCircle(xpos, ypos, radius, mPaint);
}
}