437_完整彩色星星进度条

来源:互联网 发布:都叫兽数据恢复激活码 编辑:程序博客网 时间:2024/05/29 15:14




完整彩色星星进度条


        spv_progress = (SemicircleProgressView) v.findViewById(R.id.spv_progress);
        spv_progress.setProperties(20, (0.5), DensityUtil.dip2px(mActivity, 210), DensityUtil.dip2px(mActivity, 210));
        spv_progress.start();








public class SemicircleProgressView extends View {


    private int width;
    private int height;
    private int barWidth;
    private double percent;
    private Paint paint;
    private int progress = 0;
    private int target = 0;
    private Paint whitePaint;
    private Paint pathWayPaint;
    private double radius;
    private Paint starPaint;
    private Shader shader;
    private Shader shader2;


    public SemicircleProgressView(Context context) {
        this(context, null);
    }


    public SemicircleProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public SemicircleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setProperties(int barWidth, double percent, int width, int height) {
        this.width = width;
        this.height = height;


        //这个是条条的宽度
        this.barWidth = barWidth;


        //百分比,0%是从150开始,50%是走120度,到达270,100%是走240度,到达30
        this.percent = percent;


        //从150度开始,到达30度,150-180-360-30,一共240度
        target = (int) ((240) * percent);


        //两个shader,1个radius
        shader = new LinearGradient(0, 0, 0, height / 2,
                Color.RED, Color.YELLOW, Shader.TileMode.CLAMP);
        int f11 = getResources().getColor(R.color.color_single_11FFFFFF);
        int fdd = getResources().getColor(R.color.color_single_ddFFFFFF);
        shader2 = new LinearGradient(0, 0, 0, height / 2,
                fdd, f11, Shader.TileMode.CLAMP);
        radius = width / 2;


        //设置彩色笔
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(barWidth);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setShader(shader);


        //设置后面白色背景笔
        whitePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        whitePaint.setStyle(Paint.Style.STROKE);
        whitePaint.setStrokeWidth(barWidth - 5);
        whitePaint.setStrokeCap(Paint.Cap.ROUND);
        whitePaint.setColor(getResources().getColor(R.color.color_single_66FFFFFF));


        //设置星星轨道笔
        pathWayPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        pathWayPaint.setStyle(Paint.Style.STROKE);
        pathWayPaint.setStrokeWidth(3);
        pathWayPaint.setStrokeCap(Paint.Cap.ROUND);
        pathWayPaint.setShader(shader2);


        //设置星星笔
        starPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        starPaint.setColor(Color.WHITE);
        starPaint.setMaskFilter(new BlurMaskFilter(5, BlurMaskFilter.Blur.SOLID));
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        //画出后面白色的条条背景
        drawWhiteBottomCircle(canvas, width - barWidth);


        //后面是位置,比如长宽是200,笔头是20,那么200-20,给个间隔
        drawArc(canvas, width - barWidth);


        //画出中间细的星星轨道
        drawWhiteThinCircle(canvas, width - barWidth - 25);


        //画出星星
        float x = getXCoordinate(300 - progress, radius - barWidth - 25);
        float y = getYCoordinate(300 - progress, radius - barWidth - 25);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        canvas.drawCircle(x, y, 5, starPaint);
    }


    private float getXCoordinate(int angle, double r) {
        double sin = Math.sin(Math.PI * angle / 180);
        float x = (float) (sin * r + radius);
        return x;
    }


    private float getYCoordinate(int angle, double r) {
        double cos = -Math.cos(Math.PI * angle / 180);
        float y = (float) (radius - cos * r);
        return y;
    }


    private void drawWhiteThinCircle(Canvas canvas, int position) {
        Path path = new Path();
        RectF oval = new RectF(width - position, width - position, position, position);
        path.arcTo(oval, 150, progress);
        canvas.drawPath(path, pathWayPaint);
    }


    private void drawWhiteBottomCircle(Canvas canvas, int position) {
        Path path = new Path();
        RectF oval = new RectF(width - position, width - position, position, position);
        path.arcTo(oval, 150, 240);
        canvas.drawPath(path, whitePaint);
    }


    private void drawArc(Canvas canvas, int position) {
        Path path = new Path();
        RectF oval = new RectF(width - position, width - position, position, position);
        path.arcTo(oval, 150, progress);
        canvas.drawPath(path, paint);
    }


    public void start() {
        progress = 0;
        new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                invalidate();
                if (progress >= target) {
                    this.removeCallbacksAndMessages(null);
                } else {
                    progress = progress + 3;
                    this.sendEmptyMessageDelayed(0, 6);
                }
            }
        }.sendEmptyMessage(0);
    }
}







0 0
原创粉丝点击