布局:
<MyCustomView
android:id="@+id/MyCustomView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
调用方式:
MyCustomView1.setmPaint_color1("#f7f7f7");
MyCustomView1.setOne_dushu(360);
MyCustomView1.setTwo_dushu(0);
MyCustomView1.setThree_dushu(0);
MyCustomView1.setFour_dushu(0);
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
public class MyCustomView extends View {
private int x = 30; //绘画的扇形图大小
private int y = 260; //绘画的扇形图大小
private int z = 10; //扇形图之间的半径差
private int textsize = 40; //字的大小
private int textsize_1 = -18; //字体往右的偏移量
private int one_jiaodu = -60; //第一个圆开始绘画的位置 -90为12点钟的位置 0为3点钟的位置
private int one_dushu = 90; //绘画的度数
private int two_dushu = 120; //绘画的度数
private int three_dushu = 120; //绘画的度数
private int four_dushu = 30; //绘画的度数
private String mPaint_color1 = "#52D58E"; //画笔1的颜色
private String mPaint_color2 = "#06B4FD"; //画笔2的颜色
private String mPaint_color3 = "#FC9301"; //画笔3的颜色
private String mPaint_color4 = "#FF5437"; //画笔4的颜色
private String string = "";
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyCustomView(Context context) {
super(context);
}
/**
* 设置画布的大小
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = this.width;
int height = this.height;
setMeasuredDimension(width, height);
}
int width = 340;
int height = 350;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布的颜色
canvas.drawColor(Color.WHITE);
//将画布坐标原点移到中心位置
canvas.translate(25, 25);
//创建一个画笔
Paint mPaint = new Paint();
//mPaint.setStyle(Paint.Style.STROKE);//设置画笔为空心
mPaint.setColor(Color.parseColor(mPaint_color1)); //设置画颜色
mPaint.setStrokeWidth(10);//设置画笔宽度
//创建一个画笔
Paint mPaint1 = new Paint();
mPaint1.setColor(Color.parseColor(mPaint_color2)); //设置画颜色
mPaint1.setStrokeWidth(10);//设置画笔宽度
//创建一个画笔
Paint mPaint2 = new Paint();
mPaint2.setColor(Color.parseColor(mPaint_color3)); //设置画颜色
mPaint2.setStrokeWidth(10);//设置画笔宽度
//创建一个画笔
Paint mPaint3 = new Paint();
mPaint3.setColor(Color.parseColor(mPaint_color4)); //设置画颜色
mPaint3.setStrokeWidth(10);//设置画笔宽度
/**
* 画一段实心扇形
* 第一个参数:一个矩形,用户定义扇形的大小
* 第二个参数:画扇形的起始角度
* 第三个参数:要画的角度
* 第四个参数是否包括圆形(true为包括,一般用于画扇形;false为不包括,一般用于画弧形)
* 第四个参数:画笔
*/
@SuppressLint("DrawAllocation")
RectF oval = new RectF(x, x, y, y); //x和y是计算出 三个圆的大小 包含了位置
canvas.drawArc(oval, -90 + one_jiaodu, one_dushu, true, mPaint);
if (two_dushu != 0) {
@SuppressLint("DrawAllocation")
RectF oval1 = new RectF(x - z, x - z, y + z, y + z);
canvas.drawArc(oval1, -90 + one_jiaodu + one_dushu, two_dushu, true, mPaint1);
}
if (three_dushu != 0) {
@SuppressLint("DrawAllocation")
RectF oval2 = new RectF(x - (z * 2), x - (z * 2), y + (z * 2), y + (z * 2));
canvas.drawArc(oval2, -90 + one_jiaodu + one_dushu + two_dushu, three_dushu, true, mPaint2);
}
if (four_dushu != 0) {
@SuppressLint("DrawAllocation")
RectF oval2 = new RectF(x - (z * 3), x - (z * 3), y + (z * 3), y + (z * 3));
canvas.drawArc(oval2, -90 + one_jiaodu + one_dushu + two_dushu + three_dushu, four_dushu, true, mPaint3);
}
//-----------------------------⑤画一个实心圆--------------------------------
@SuppressLint("DrawAllocation") Paint circle = new Paint();
circle.setColor(Color.parseColor("#ffffff")); //设置画颜色
circle.setStrokeWidth(10);//设置画笔宽度
circle.setStyle(Paint.Style.FILL);
canvas.drawCircle((y - x) / 2 + x, (y - x) / 2 + x, 90, circle);
@SuppressLint("DrawAllocation") TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(getResources().getColor(R.color.c999999));
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(textsize); //文字的大小
String text = string;
@SuppressLint("DrawAllocation") StaticLayout staticLayout = new StaticLayout(text, textPaint,
500, Layout.Alignment.ALIGN_NORMAL,
1.0f, 0.0f, false);
canvas.translate((y - x) / 2 - x - 5 + textsize_1, (y - x) / 2 + x - (textsize / 2));
staticLayout.draw(canvas);
}
public void setmPaint_color1(String mPaint_color1) {
this.mPaint_color1 = mPaint_color1;
}
public void setmPaint_color2(String mPaint_color2) {
this.mPaint_color2 = mPaint_color2;
}
public void setmPaint_color3(String mPaint_color3) {
this.mPaint_color3 = mPaint_color3;
}
public void setmPaint_color4(String mPaint_color4) {
this.mPaint_color4 = mPaint_color4;
}
public void setTextsize_1(int textsize_1) {
this.textsize_1 = textsize_1;
}
public void setString(String string) {
this.string = string;
}
public void setOne_dushu(int one_dushu) {
this.one_dushu = one_dushu;
}
public void setOne_jiaodu(int one_jiaodu) {
this.one_jiaodu = one_jiaodu;
}
public void setTextsize(int textsize) {
this.textsize = textsize;
}
public void setZ(int z) {
this.z = z;
}
public void setTwo_dushu(int two_dushu) {
this.two_dushu = two_dushu;
}
public void setThree_dushu(int three_dushu) {
this.three_dushu = three_dushu;
}
public void setFour_dushu(int four_dushu) {
this.four_dushu = four_dushu;
}
}
/**
* 最一开始测试的数据
*/
// RectF oval = new RectF(100, 100, 400, 400);
// canvas.drawArc(oval, -150, 100, true, mPaint);
//
// RectF oval1 = new RectF(80, 80, 420, 420);
// canvas.drawArc(oval1, -50, 200, true, mPaint1);
//
// RectF oval2 = new RectF(60, 60, 440, 440);
// canvas.drawArc(oval2, 150, 60, true, mPaint2);
//
//
// //-----------------------------⑤画一个实心圆--------------------------------
// Paint circle = new Paint();
// circle.setColor(Color.parseColor("#ffffff")); //设置画颜色
// circle.setStrokeWidth(10);//设置画笔宽度
// circle.setStyle(Paint.Style.FILL);
// canvas.drawCircle(250, 250, 100, circle);
// TextPaint textPaint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
// textPaint.setColor(Color.RED);
// textPaint.setStyle(Paint.Style.FILL);
// textPaint.setTextSize(35);
// String text="测试的文字";
// StaticLayout staticLayout=new StaticLayout(text, textPaint,
// 500, Layout.Alignment.ALIGN_NORMAL,
// 1.0f,0.0f , false);
// canvas.translate(165,220);
// staticLayout.draw(canvas);
-----------------------------①画一个空心正方形--------------------------------
///**
// * 创建一个矩形
// * 第一个参数:矩形左顶点的X轴坐标
// * 第二个参数:矩形左顶点的Y轴坐标
// * 第三个参数:矩形右下角顶点的X轴坐标
// * 第四个参数:矩形右下角顶点的Y轴坐标
// */
// Rect mRect = new Rect(10, 10, 50, 50);
开始画矩形了
// canvas.drawRect(mRect, mPaint);
//
-----------------------------②画一个实心正方形--------------------------------
设置画笔为实心
// mPaint.setStyle(Paint.Style.FILL);
重新设置矩形的大小
// mRect.set(60, 60, 100, 100);
// canvas.drawRect(mRect, mPaint);
//
-----------------------------③画一个空心长方形--------------------------------
设置画笔的宽度
// mPaint.setStrokeWidth(2);
// mPaint.setStyle(Paint.Style.STROKE);
// mRect.set(70, 110, 140, 140);
// canvas.drawRect(mRect, mPaint);
//
-----------------------------④画一个空心圆--------------------------------
消除锯齿
// mPaint.setAntiAlias(true);
///**
// * 画一个空心圆
// * 第一个参数:圆心的X轴坐标
// * 第二个参数:圆心的Y轴坐标
// * 第三个参数:圆的半径
// * 第四个参数:画笔
// */
// canvas.drawCircle(180, 180, 20, mPaint);
//
//-----------------------------⑥画一段实心扇形--------------------------------
//先画一个矩形
-----------------------------⑦画一段空心扇形--------------------------------
// mPaint.setStyle(Paint.Style.STROKE);
// oval.set(290, 290, 350, 350);
// canvas.drawArc(oval, 0, 60, true, mPaint);
//
-----------------------------⑧画一段弧--------------------------------
// oval.set(360, 360, 400, 400);
// canvas.drawArc(oval, 0, 70, false, mPaint);
//
-----------------------------⑨画一个内切圆--------------------------------
// oval.set(360, 360, 400, 400);
// canvas.drawRect(oval, mPaint);
// canvas.drawArc(oval, 0, 360, false, mPaint);