Android 最简单仿IOS菊花加载样式,自定义Loading加载View

自定义菊花旋转加载view,可自定义半径大小,旋转view个数、颜色等,具体代码如下,可直接使用

public class LoadingView extends View {
    private RectF rectF;
    private Paint paint;
    private int radius =52; //半径
    private int count = 0;
    private boolean run = false; //动画控制

    public LoadingView(Context context) {
        super(context);
        init();
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {

        super(context, attrs);

        init();

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(radius * 2, radius * 2);
    }

    private void init() {

        paint = new Paint();

        paint.setAntiAlias(true);

        paint.setColor(Color.WHITE);

        //可根据自己需求调整loading大小
        rectF = new RectF(radius - 8, 23, radius+2 , 40);

    }

    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvas.rotate(count * 45, radius, radius);

//设置loading加载旋转个数
        for (int i = 0; i < 8; i++) {

            paint.setAlpha(255 - i * 20);
            canvas.drawRoundRect(rectF, 20, 20, paint);
            canvas.rotate(45, radius, radius);
        }
        count++;
        if (count > 7) {
            count = 0;
        }
        if (run) {
            postInvalidateDelayed(200);
        }
    }

    public void start() {
        if (!run) {
            postInvalidateDelayed(200);
            run = true;
        }
    }

    public void stop() {
        run = false;
    }

}

Xml中调用如下

  <com。view.LoadingView
                  android:id="@+id/loading"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerVertical="true"
                  android:layout_marginLeft="50dp"
              />

样式如下图

Android 最简单仿IOS菊花加载样式,自定义Loading加载View_第1张图片

你可能感兴趣的:(Android基础,android,ios)