android 实现圆形进度条

要实现圆形进度条只能使用自定义view了,实现代码比较简单,自定义view代码

package com.example.apple.memory;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by apple on 17/5/26.
 */

public class CircleProgressView extends View {

    private int mMaxProgress = 100;
    private int Progress = 0;
    private int Progress1 = 0;
    private int Progress2 = 0;

    private final int mCircleLineStrokeWidth = 100;//设置圆形画笔宽度

    private final int mTxtStrokeWidth =  2;//设置文字画笔宽度

    // 画圆所在的距形区域
    private  RectF mRectF;
    private  Paint mPaint;



    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs,0);

        mRectF = new RectF();
        mPaint = new Paint();
    }


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


        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        canvas.drawColor(Color.TRANSPARENT);
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);

        // 位置
        mRectF.left = mCircleLineStrokeWidth / 2;
        mRectF.top = mCircleLineStrokeWidth / 2;
        mRectF.right = width - mCircleLineStrokeWidth / 2;
        mRectF.bottom = height - mCircleLineStrokeWidth / 2;

        // 绘制圆圈,进度条背景
        canvas.drawArc(mRectF, -180, 360, false, mPaint);
        mPaint.setColor(Color.RED);
        canvas.drawArc(mRectF, -180, ((float) Progress / mMaxProgress) * 360, false, mPaint);


        //绘制第二段长度
        mPaint.setColor(Color.BLUE);
        canvas.drawArc(mRectF,-180+ ((float) Progress / mMaxProgress) * 360, ((float) Progress1 / mMaxProgress) * 360, false, mPaint);

        //绘制第三段长度
        mPaint.setColor(Color.BLACK);
        canvas.drawArc(mRectF,-180+ ((float) (Progress+Progress1) / mMaxProgress) * 360, ((float) Progress2 / mMaxProgress) * 360, false, mPaint);



        //绘制百分百
        mPaint.setStrokeWidth(mTxtStrokeWidth);
        String text = Progress + "%";
        int textHeight = height / 4;
        mPaint.setTextSize(textHeight);
        int textWidth = (int) mPaint.measureText(text, 0, text.length());
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, mPaint);

    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.Progress = progress;
        this.invalidate();
    }

    public void setProgress1(int progress) {
        this.Progress1 = progress;
        this.invalidate();
    }

    public void setProgress2(int progress) {
        this.Progress2 = progress;
        this.invalidate();
    }

}


2、引用的话首先布局中直接添加就好

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.apple.memory.MainActivity">

    <com.example.apple.memory.CircleProgressView
        android:id="@+id/circleProgressBar"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true" />
</RelativeLayout>
3、代码中使用,为了看到有个动画效果所以加了个定时器,可以看到慢慢增加到指定值。

package com.example.apple.memory;

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

public class MainActivity extends AppCompatActivity {

    private CircleProgressView circleProgressView;
    private final int UPDATE_PROGRESS = 0;
    private int number = 0;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case UPDATE_PROGRESS:

                    circleProgressView.setProgress(2 + number);

                    circleProgressView.setProgress1(10 + number);

                    circleProgressView.setProgress2(20 + number);

                    break;
            }

            super.handleMessage(msg);

        }
    };

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

        initView();
    }

    private void initView() {
        circleProgressView = (CircleProgressView) findViewById(R.id.circleProgressBar);
        circleProgressView.setMaxProgress(100);

        new TestThread().start();
    }

    class TestThread extends Thread {

        @Override
        public void run() {
            while (number < 20) {
                number++;
                mHandler.sendEmptyMessage(UPDATE_PROGRESS);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(0);
            mHandler = null;

        }
        super.onDestroy();
    }
}

实现效果


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值