圆环进度条

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="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.yuanhuanjindu.MainActivity">

   <com.example.yuanhuanjindu.jindutiao
       android:id="@+id/yuanhuan"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@color/colorAccent"
       />

</android.support.constraint.ConstraintLayout>


/主函数/
 
package com.example.yuanhuanjindu;

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

public class MainActivity extends AppCompatActivity {

    private jindutiao jin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      jin= (jindutiao) findViewById(R.id.yuanhuan);

        //开启子线程
      new Thread(jin).start();
        
    }
}
/圆环的类 //

package com.example.yuanhuanjindu;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
/**
 * Created by Administrator on 2017/11/29 0029.
 */




@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public class jindutiao extends View implements  Runnable {
    //圆的笔
    private Paint mpaint;
    //圆环的笔
    private Paint mpaint2;
    //定义初始进度
    private int i = 0;
    //定义总进度
    private int o = 100;

    //内部圆环半径
   private int  ao=100;
    //默认圆环宽度
  private int  yuanwid=20;

    //文字画笔
    private Paint wen;
    public jindutiao(Context context) {
        super(context);
        init();
    }

    public jindutiao(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public jindutiao(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //        mpaint = new Paint();

        mpaint.setColor(Color.parseColor("#FF8080"));

        //圆环
        mpaint2 = new Paint();
        mpaint2.setColor(Color.parseColor("#9569F8"));
        //设置圆环是否填充圆
        mpaint2.setStyle(Paint.Style.STROKE);
        //设置圆环宽度
        mpaint2.setStrokeWidth(50);
        //文字画笔
        wen=new Paint();
        wen.setTextSize(30);
    }
    @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 heightSize=MeasureSpec.getSize(heightMeasureSpec);
       //定义变量记录控件的宽高
         int  width=0;
         int height=0;
        switch (widthMode){
            case MeasureSpec.UNSPECIFIED:{
                //很少用
            }
            break;
            case MeasureSpec.AT_MOST:{
              width=ao*2+yuanwid*2;
            }
            break;
            case MeasureSpec.EXACTLY:{
                width=widthSize;
            }
            break;
        }
       switch(heightMode){
           case MeasureSpec.UNSPECIFIED:{
           }
           break;
           case MeasureSpec.AT_MOST:{
            //精确模式
            height=ao*2+yuanwid*2;
           }
           break;
           case MeasureSpec.EXACTLY:{
           height=heightSize;
           }
           break;
       }
       setMeasuredDimension(width,height);
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画圆和圆环
        yuan(canvas);
        yuanhuan(canvas);
        textd(canvas);
    }
    //    private void yuan(Canvas canvas) {
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 100, mpaint);
    }
    //圆环
    private void yuanhuan(Canvas canvas) {                                                              //-90代表从顶部开始出现圆环  (i*360)/o代表结束位置 可控
        canvas.drawArc(getWidth() / 2 - ao, getHeight() / 2 - ao, getWidth() / 2 + ao, getHeight() / 2 + ao,-90,(i*360)/o,false, mpaint2);
    }
    //文本
    private void textd(Canvas canvas){
        String jindu=i+"%";

     canvas.drawText(jindu,getWidth()/2-25,getHeight()/2,wen);
    }
    @Override
    public void run() {
        while (true) {
            if (i < 100) {
                i++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //在子线程中重新绘制
                postInvalidate();
            }else{
                i=0;
            }
        }
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值