@Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(widthMeasureSpec, widthMeasureSpec); }
(2) 再画一个圆形,这个蓝色的圆形就是我们可见的视图。
(3) 获取圆形的半径,半径为circleRadius=width/2。即半径50(图中的三条线为圆的半径)
@Override protected void onDraw(Canvas canvas) { //计算圆的半径 circleRadius = getWidth() / 2; ////绘制正方形/圆形/圆矩形(注意绘制此图形的目的是用来作为背景)。 drawView(canvas , paintTemp) ; //得到圆内接矩形的长度的一半 int rectangleHalf = (int) Math. sqrt(( circleRadius * circleRadius ) / 2) ; //左对角线的坐标 int leftDiagonalStartX, leftDiagonalStartY , leftDiagonalEndX, leftDiagonalEndY; //计算左对角线的坐标(需要考虑paintWidth和diagonalLength对对角线的影响,觉得这两个参数影响代码阅读可以暂时去掉) leftDiagonalStartX = leftDiagonalStartY = circleRadius - rectangleHalf + paintWidth + diagonalLength ; leftDiagonalEndX = leftDiagonalEndY = circleRadius + rectangleHalf - paintWidth - diagonalLength; //绘制左对角线 drawDiagonal(canvas , leftDiagonalStartX, leftDiagonalStartY, leftDiagonalEndX , leftDiagonalEndY, diagonalLeftColor , paintCapRound) ; //右对角线的坐标 int rightDiagonalStartX, rightDiagonalStartY , rightDiagonalEndX, rightDigonalEndY; //计算右对角线的坐标(需要考虑paintWidth和diagonalLength对对角线的影响,觉得这两个参数影响代码阅读可以暂时去掉) rightDiagonalStartX = circleRadius + rectangleHalf - paintWidth - diagonalLength; rightDiagonalStartY = circleRadius - rectangleHalf + paintWidth + diagonalLength; rightDiagonalEndX = circleRadius - rectangleHalf + paintWidth + diagonalLength; rightDigonalEndY = circleRadius + rectangleHalf - paintWidth - diagonalLength; //绘制右对角线 drawDiagonal(canvas , rightDiagonalStartX, rightDiagonalStartY, rightDiagonalEndX, rightDigonalEndY , diagonalRightColor, paintCapRound); //绘制正方形/圆形/圆矩形 drawView(canvas , mPaint) ; } /** * 绘制视图 * * @param canvas * @param paint */ public void drawView(Canvas canvas , Paint paint) { if (cancelViewType == SQUARE) { canvas.drawRect(paintWidth , paintWidth, getWidth() - paintWidth, getHeight() - paintWidth, paint) ; } else if (cancelViewType == CIRCLE) { canvas.drawCircle(circleRadius , circleRadius, circleRadius - paintWidth , paint); } else if (cancelViewType == ROUNDED_RECTANGLE) { canvas.drawRoundRect(new RectF(paintWidth, paintWidth, getWidth() - paintWidth, getHeight() - paintWidth), roundDegree, roundDegree , paint); } else { Log.e( "------------->", "视图类型错误!" ); } } /** * 绘制对角线 * * @param canvas * @param diagonalStartX * @param diagonalStartY * @param diagonalEndX * @param diagonalEndY */ public void drawDiagonal(Canvas canvas , int diagonalStartX, int diagonalStartY, int diagonalEndX, int diagonalEndY , int diagonalColor, boolean paintCapRound) { Paint diagonalPaint = new Paint(); diagonalPaint.setAntiAlias( true); diagonalPaint.setColor(diagonalColor) ; diagonalPaint.setStrokeWidth( diagonalPaintWidth); diagonalPaint.setStyle(Paint.Style. STROKE); if (paintCapRound ) { //设置画笔的半圆风格 diagonalPaint.setStrokeCap(Paint.Cap. ROUND); } else { //设置画笔默认值 diagonalPaint.setStrokeCap(Paint.Cap. SQUARE); } //绘制对角线 canvas.drawLine(diagonalStartX , diagonalStartY, diagonalEndX, diagonalEndY , diagonalPaint); }
<? xml version="1.0" encoding= "utf-8"?> <LinearLayout 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" android :background="#d4d4d4" android :orientation="vertical"> <TextView android :layout_width="match_parent" android :layout_height="wrap_content" android :padding="10dp" android :text="正方形,圆形,圆矩形" android :textColor="#000000" android :textSize="16dp" /> <LinearLayout android :layout_width="match_parent" android :layout_height="wrap_content" android :orientation="horizontal"> <per.edward.ui.CustomCancelView android :id="@+id/cancel_one" android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" /> <per.edward.ui.CustomCancelView android :id="@+id/cancel_two" android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Type="circle" /> <per.edward.ui.CustomCancelView android :id="@+id/cancel_three" android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Round_Degree="35" app :CC_Type="rounded_rectangle" /> </LinearLayout> <TextView android :layout_width="match_parent" android :layout_height="wrap_content" android :padding="10dp" android :text="正方形(改变了边框的颜色),圆形(改变了背景颜色),圆矩形(改变了对角线的颜色)" android :textColor="#000000" android :textSize="16dp" /> <LinearLayout android :layout_width="match_parent" android :layout_height="wrap_content" android :orientation="horizontal"> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Stroke_Width="3dp" app :CC_Stroke_Width_Color="#31b2f7" /> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Background="#7ccf66" app :CC_Type="circle" /> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Diagonal_Left_Color="#3af700" app :CC_Diagonal_Right_Color="#fff200" app :CC_Round_Degree="35" app :CC_Type="rounded_rectangle" /> </LinearLayout> <TextView android :layout_width="match_parent" android :layout_height="wrap_content" android :padding="10dp" android :text="正方形(边框和背景设置为透明),圆形(对角线变宽了,并且对角线末端为圆角),圆矩形(对角线的末端为矩形)" android :textColor="#000000" android :textSize="16dp" /> <LinearLayout android :layout_width="match_parent" android :layout_height="wrap_content" android :orientation="horizontal"> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Background="#00000000" app :CC_Diagonal_Length ="10dp" app :CC_Stroke_Width_Color="#00000000" /> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Background="#e079ead7" app :CC_Diagonal_Length ="10dp" app :CC_Diagonal_Paint_Width="10dp" app :CC_Type="circle" /> <per.edward.ui.CustomCancelView android :layout_width="80dp" android :layout_height="80dp" android :layout_margin="10dp" app :CC_Diagonal_Left_Color="#596dd1" app :CC_Diagonal_Length ="10dp" app :CC_Diagonal_Paint_Width="10dp" app :CC_Diagonal_Right_Color="#bd79c4" app :CC_Paint_Cap_Round="false" app :CC_Round_Degree="35" app :CC_Type="rounded_rectangle" /> </LinearLayout> </LinearLayout>