5

我需要实现一个圆形进度条,以便在 Fresco 下载图像时显示和更新。该类必须按照 Fresco 的方法 setProgressBarImage() 的要求从 Drawable 扩展。

我的班级正在使用 Fresco 加载图像,如下所示:

SimpleDraweeView simpleDraweeView = (SimpleDraweeView) view.findViewById(R.id.image);
simpleDraweeView.getHierarchy().setProgressBarImage(new ProgressBarDrawable());
simpleDraweeView.setImageURI(message.getMessageImage().getImageFileUriForList());

“图像” SimpleDraweeView 的 XML 如下:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/image"
    android:layout_width="192dp"
    android:layout_height="200dp"
    android:layout_margin="7dp"
    android:layout_gravity="center"
    fresco:actualImageScaleType="fitCenter"
    tools:background="@drawable/gallery_attach_dialog" />

问题是我需要用一个圆形的替换这个标准的水平进度条。而且 Fresco 不提供可绘制的圆形进度条。

有人对此有实施想法吗?

4

2 回答 2

4

你可以实现Drawable,因为ProgressBarDrawable只是实现和覆盖超级方法。如前所述,此问题应视为重复。

public class ImageLoadProgressBar extends ProgressBarDrawable {


float level;

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

int color = whatevercolorresource;

final RectF oval = new RectF();

int radius = whateverradius;

public ImageLoadProgressBar(){
    paint.setStrokeWidth(whateveintyouputhere);
    paint.setStyle(Paint.Style.STROKE);
}

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

@Override
public void draw(Canvas canvas) {
    oval.set(canvas.getWidth() / 2 - radius, canvas.getHeight() / 2 - radius,
            canvas.getWidth() / 2 + radius, canvas.getHeight() / 2 + radius);

    drawCircle(canvas, level, color);
}


private void drawCircle(Canvas canvas, float level, int color) {
    paint.setColor(color);
    float angle;
    angle = 360 / 1f;
    angle = level * angle;
    canvas.drawArc(oval, 0, Math.round(angle), false, paint);
}

}
于 2016-01-21T12:47:32.340 回答
2

您可以将 Fresco 库示例中给出的代码用于 CircularProgressDrawable。 https://github.com/facebook/fresco/blob/master/samples/contrib/com/facebook/drawee/drawable/CircleProgressBarDrawable.java

但是上面的代码中有一个问题是椭圆而不是圆形,要解决这个问题,您可以使用以下代码。

https://gist.github.com/sheerazam/ef701a564624d5f6d6c632e0d254cd15

于 2016-10-06T11:15:58.387 回答