/**
* RxJava实现加载loading效果图.
* @param imageView 要显示的ImageView
* @param color loading颜色
* @param count 圆点的数量
* @param period 动画播放间隔,单位为毫秒
* @return 返回Disposable,用于取消动画
*/
public static Disposable loading(final ImageView imageView, @ColorInt int color, final int count, long period) {
final Bitmap bitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_4444);
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(color);
canvas.translate(bitmap.getWidth() / 2, bitmap.getHeight() / 2);
imageView.setImageBitmap(bitmap);
final float angle = 360.0F / count;
final int radius = 60;
return Observable.interval(period, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.rotate(angle);
for (int i = 0; i < count; i++) {
canvas.drawCircle(200, 0, radius - 5 * i, paint);
canvas.rotate(angle);
}
imageView.setImageBitmap(bitmap);
}
});
}
效果图如下所示:
loading.gif
网友评论