美文网首页iOS
仿ios系统加载动画(菊花旋转)

仿ios系统加载动画(菊花旋转)

作者: 一花亦城 | 来源:发表于2018-07-04 15:23 被阅读95次

    无需添加图片,通过自定义view方式绘制菊花图,代码极简

    效果图:

    image

    一、自定义loadingview:

    public class LoadingView extends View {

    private RectF rectF;
    
    private Paint paint;
    
    private int radius = 90; //半径
    
    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);
        rectF = new RectF(radius - 5, 30, radius + 5, 60);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.rotate(count * 36, radius, radius);
        for (int i = 0; i < 10; i++) {
            paint.setAlpha(255 - i * 20);
            canvas.drawRoundRect(rectF, 10, 10, paint);
            canvas.rotate(36, radius, radius);
        }
        count++;
        if (count > 9) {
            count = 0;
        }
        if (run) {
            postInvalidateDelayed(100);
        }
    }
    
    public void start() {
        if (!run) {
            postInvalidateDelayed(100);
            run = true;
        }
    }
    public void stop() {
        run = false;
    }
    

    }

    二、dialog调用方法:

    布局设置

    image

    dialog配置:

    image

    然后就可以像普通dialog那样使用啦

    相关文章

      网友评论

        本文标题:仿ios系统加载动画(菊花旋转)

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