美文网首页Android常用功能
RxJava实现自定义Loading加载效果

RxJava实现自定义Loading加载效果

作者: 龙儿筝 | 来源:发表于2017-12-15 11:20 被阅读11次
    /**
       * 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

    相关文章

      网友评论

      本文标题:RxJava实现自定义Loading加载效果

      本文链接:https://www.haomeiwen.com/subject/syirwxtx.html