IT狗

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();    }}

完成后果


此文由 IT狗 编辑,本网站所发布展示的作品/文章版权归原作者所有,任何商业用途均须联系作者!

相关推荐

评论 暂无评论