中间实心球外面一个绕着实心球的圆

效果图

自定义view
package com.example.zdy_view3;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author:Created by MingShao on 2017/11/30.
 */

public class CircleProgressView extends View implements Runnable{

    private Paint paint;
    private Paint paint2;
    int progress =0;
    int totalprogress=100;
    int innerCircleRadius=100;
    int outCircleWidth=20;
    private Paint textpaint;
    int centerX;
    int centerY;
    int outRingRadius;
    private RectF rectF;


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

    public CircleProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleProgressView, defStyleAttr, 0);
        innerCircleRadius = ta.getInt(R.styleable.CircleProgressView_innerCircleRadius, innerCircleRadius);
        init();
        ta.recycle();
    }


    //创建一个初始化画笔的方法
    private void init() {
        paint = new Paint();
        paint.setColor(Color.YELLOW);
paint.setAntiAlias(true);
        paint2 = new Paint();
        paint2.setColor(Color.RED);
        paint2.setStyle(Paint.Style.STROKE);

        paint2.setStrokeWidth(outCircleWidth);
        paint2.setAntiAlias(true);
        paint2.setStrokeCap(Paint.Cap.ROUND);
        textpaint = new Paint();
        textpaint.setTextSize(35);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /*
        测量的三中方式
        精确地
        制度哦
        不确定:用的很少,一般很少用到它
         */
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        int widthsize=MeasureSpec.getSize(widthMeasureSpec);
        int heighsize=MeasureSpec.getSize(heightMeasureSpec);
        int width=0;
        int height=0;
        switch (widthMode){
            case MeasureSpec.UNSPECIFIED:{

            }
            break;
            case MeasureSpec.AT_MOST:{
                width=innerCircleRadius*2+outCircleWidth*2;
            }
            break;
            case MeasureSpec.EXACTLY:{
            width=widthsize;

            }
            break;

        }
        switch (heightMode){
            case MeasureSpec.UNSPECIFIED:
            {

            }break;
            case MeasureSpec.AT_MOST:{
                height=innerCircleRadius*2+outCircleWidth*2;
            }
            break;
            case MeasureSpec.EXACTLY:{
                height=heighsize;
            }
            break;
        }
        setMeasuredDimension(width,height);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX=w/2;
        centerY=h/2;

        outRingRadius=innerCircleRadius+outCircleWidth/2;
        rectF = new RectF();
        rectF.left=centerX-outRingRadius;
        rectF.top=centerY-outRingRadius;
        rectF.right=centerX+outRingRadius;
        rectF.bottom=centerY+outRingRadius;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    /*
             这是一个描绘图形的方法
              */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawInnerCircl(canvas);
        drawOutCircleRing(canvas);
        dreawText(canvas);
    }

    private void dreawText(Canvas canvas) {
        String des = progress + "%";
        Paint.FontMetrics fontMetrics = new Paint.FontMetrics();
        int textwidth= (int) Math.ceil(fontMetrics.descent-fontMetrics.ascent);
        int height= (int) textpaint.measureText(des,0,des.length());
        canvas.drawText(des,getWidth()/2-(textwidth/2),getHeight()/2-(height/2),textpaint);
    }


    //画外部圆的方法
    private void drawOutCircleRing(Canvas canvas) {
        canvas.drawArc(rectF,-90,(progress *360)/totalprogress,false,paint2);
    }

    /*
    画内部是新的方法
     */
    private void drawInnerCircl(Canvas canvas) {
        canvas.drawCircle(getWidth()/2,getHeight()/2,innerCircleRadius,paint);
    }

    @Override
    public void run() {
        while (true){
            if(progress<100){
                progress++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                postInvalidate();
            }else{
                progress=0;
            }
        }
    }
}
需要实现的xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleProgressView">
        <attr name="innerCircleRadius" format="integer"></attr>
    </declare-styleable>
</resources>
主方法
package com.example.zdy_view3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CircleProgressView circleProgressView= (CircleProgressView) findViewById(R.id.circleview);
        new Thread(circleProgressView).start();
    }
}
主方法布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:test="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.zdy_view3.MainActivity">
    <com.example.zdy_view3.CircleProgressView
        android:id="@+id/circleview"
        test:innerCircleRadius="300"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f1235618"
        />


</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值