Android 自定义view进度条
创建自定义view继承view
package com.example.jindu.ui.weight;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class ProgressView extends View {
private Context context;
private int height, width; //自定义控件的宽高
private float progress; //进度
private Paint paint; //蓝色扇形所需的画笔
private Paint bkPaint; //白色圆形所需的画笔
private Paint tvPaint; //圆里面的进度字所需的画笔
private Rect mBound; //用于获取字体的大小
public ProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
this.paint = new Paint();
this.bkPaint = new Paint();
this.tvPaint = new Paint();
this.mBound = new Rect();
init();
}
private void init() {
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
bkPaint.setStyle(Paint.Style.FILL);
bkPaint.setColor(Color.BLUE);
bkPaint.setAntiAlias(true);
tvPaint.setColor(Color.BLACK);
tvPaint.setTextSize(30);
}
//获取当前控件的高度和宽度,单位是像素
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
}
//获取到了宽高后我们就可以开始画了,在CircleProgressBar 的onDraw方法里面画扇形,圆形,字。
private float set2Degree(float sendFt) { //将进度的数值变为弧度数值,进度100,弧度有360,所以比例是3.6
return sendFt * 3.6f;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (width * height == 0) {
return;
}
canvas.drawArc(new RectF(0, 0, width, height), 270, set2Degree(progress), true, paint);
//画蓝色扇形
canvas.drawCircle(width / 2, height / 2, width / 2 - 15, bkPaint); //画绿色圆形,半径比蓝色扇形的小5px
if (progress < 100) { //进度没达到100%时显示进度
String strPro = String.valueOf((int) progress) + "%";
tvPaint.getTextBounds(strPro, 0, strPro.length(), mBound);
canvas.drawText(strPro, width / 2 - mBound.width() / 2, height / 2 + mBound.height()
/ 2, tvPaint);
} else { //达到100%后显示完成
String text = "完成";
tvPaint.getTextBounds(text, 0, text.length(), mBound);
canvas.drawText(text, width / 2 - mBound.width() / 2, height / 2 + mBound.height() /
2, tvPaint);
}
}
//更新弧度,在CircleProgressBar里面加个public方法来实时更新进度。--进度发生改变后调用改方法修改进度
public void setProgress(float progress) {
this.progress = progress;
postInvalidate();
}
}
布局引入view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".ui.activity.MainActivity">
<com.example.jindu.ui.weight.ProgressView
android:id="@+id/circle_progress"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
主方法Activity运用Handler
package com.example.jindu.ui.activity;
import android.animation.AnimatorSet;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.jindu.ui.weight.ProgressView;
import com.example.jindu.R;
public class MainActivity extends AppCompatActivity {
private ProgressView circleProgressBar;
private AnimatorSet animatorSet;
private float progress = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (progress <= 99) {
// animatorSet.start();
++progress;
circleProgressBar.setProgress(progress); //更新进度条
sendEmptyMessageDelayed(1, 100);
}
if (progress == 100) {
startActivity(new Intent(MainActivity.this, TwoActivity.class));
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
circleProgressBar = findViewById(R.id.circle_progress);
//发送消息
handler.sendEmptyMessageDelayed(1, 100);
}
}
效果实现