zoukankan      html  css  js  c++  java
  • 一个简单的Loading控件

    实现效果如下:

    使用方法:

    在layout文件中添加以下代码:

       <com.example.jack.ui.widget.RingLoading
            android:layout_width="20.0dip"
            android:layout_height="20.0dip"
       />

    具体代码如下:

    package com.example.jack.ui.widget;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.SweepGradient;
    import android.util.AttributeSet;
    import android.view.View;
    
    /**
     * Created by pengf on 2017/4/10.
     */
    
    public class RingLoading extends View
    {
        private static final int PROGRESS = 5;
        private int centerX;
        private int centerY;
        private Context context;
        private int mProgressColor ;
        private Matrix matrix;
        private final Paint paint;
        private int roundWidth = 10;
        private int start = 0;
    
        public RingLoading(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            this.context = context;
            this.paint = new Paint();
            this.paint.setAntiAlias(true);
            this.paint.setStyle(Paint.Style.STROKE);
            this.paint.setStrokeWidth(this.roundWidth);
            this.mProgressColor =Color.parseColor("#ffffcc00");
    
        }
    
        @Override
        protected void onDraw(Canvas canvas)
        {
            int center = getWidth() / 2;
            int radius = center - this.roundWidth / 2;
            SweepGradient sweepGradient = new SweepGradient(this.centerX, this.centerY, Color.TRANSPARENT, this.mProgressColor);
            this.paint.setShader(sweepGradient);
            this.matrix = new Matrix();
            this.matrix.postRotate(this.start, this.centerX, this.centerY);
            canvas.concat(this.matrix);
            canvas.drawCircle(center, center, radius, this.paint);
            this.start = (PROGRESS + this.start);
            invalidate();
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh)
        {
            super.onSizeChanged(w, h, oldw, oldh);
            this.centerX = (w / 2);
            this.centerY = (h / 2);
        }
    }
    
  • 相关阅读:
    IOS Block-Block块的使用与理解
    IOS 多线程03-GCD
    IOS 多线程01-线程基础知识
    JavaScript高级-定义函数(类)方法
    互联网技术笔试总通不过?leetcode刷对了么
    Redis 内存满了怎么办? Redis的内存淘汰策略
    SpringBoot项目优化和Jvm调优
    中台的末路
    Java 应用中的日志
    Spring Boot 支持https
  • 原文地址:https://www.cnblogs.com/rainboy2010/p/6692499.html
Copyright © 2011-2022 走看看