package com.example.myapplication.mycustom
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View
import kotlin.math.cos
import kotlin.math.sin
class Pie @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
val radius1 = 200
val radius2 = 300
val paint = Paint()
val path = Path()
init {
paint.strokeWidth = 16f
paint.strokeJoin = Paint.Join.ROUND
paint.color = Color.BLUE
paint.style = Paint.Style.FILL_AND_STROKE
paint.strokeCap = Paint.Cap.ROUND
paint.isAntiAlias = true
}
override fun onDraw(canvas: Canvas?) {
canvas?.drawArc(100f,100f,500f,500f,0f,90f,false,paint)
// val r1StartX = 300 + radius1 * cos(Math.toRadians(180.0))
// val r1StartY = 300 + radius1 * sin(Math.toRadians(180.0))
// val r1EndX = 300 + radius1 * cos(Math.toRadians(0.0))
// val r1EndY = 300 + radius1 * sin(Math.toRadians(0.0))
//
// val endX2 = 300 + radius2 * cos(Math.toRadians(0.0))
// val endY2 = 300 + radius2 * sin(Math.toRadians(0.0))
// path.addArc(0f, 0f, 600f, 600f, 0f, 180f)
// path.lineTo(r1StartX.toFloat(), r1StartY.toFloat())
// path.addArc(100f, 100f, 500f, 500f, 180f, -180f)
// path.lineTo(endX2.toFloat(), endY2.toFloat())
// canvas?.drawPath(path, paint)
//canvas?.translate(300f,300f)
// canvas?.save()
val i = 20f
val r1StartX = 300f + i * cos(Math.toRadians(30.0))
val r1StartY = 300f + i * sin(Math.toRadians(30.0))
Log.i("DBFF", "onDraw: r1StartX=r1StartY")
canvas?.drawPoint(300f, 300f, paint);
canvas?.save()
canvas?.translate((r1StartX - 300f).toFloat(), (r1StartY - 300f).toFloat())
draMyArc(canvas!!, PointF(300f, 300f), 250f, 150f, 0f, 60f)
canvas.restore()
val r2StartX = 300f+ i * cos(Math.toRadians(105.0))
val r2StartY = 300f + i * sin(Math.toRadians(105.0))
Log.i("DBFF", "onDraw: r2StartX=r2StartY")
paint.color = Color.RED
canvas.save()
canvas?.translate((r2StartX - 300f).toFloat(), (r2StartY - 300f).toFloat())
draMyArc(canvas!!, PointF(300f, 300f), 250f, 150f, 60f, 90f)
canvas.restore()
val r3StartX = 300f + i * cos(Math.toRadians(195.0))
val r3StartY = 300f + i * sin(Math.toRadians(195.0))
Log.i("DBFF", "onDraw: r3StartX=r3StartY")
paint.color = Color.GREEN
canvas.save()
canvas?.translate((r3StartX - 300f).toFloat(), (r3StartY - 300f).toFloat())
draMyArc(canvas!!, PointF(300f, 300f), 250f, 150f, 150f, 90f)
canvas.restore()
val r4StartX = 300f + i * cos(Math.toRadians(290.0))
val r4StartY = 300f + i * sin(Math.toRadians(290.0))
Log.i("DBFF", "onDraw: r3StartX=$r4StartX")
Log.i("DBFF", "onDraw: r4StartY=$r4StartY")
// paint.color = Color.BLACK
canvas.save()
canvas?.translate((r4StartX - 300).toFloat(), (r4StartY - 300f).toFloat())
draMyArc(canvas!!, PointF(300f, 300f), 250f, 150f, 240f, 100f)
canvas.restore()
// draMyArc(canvas!!,PointF(300f,300f),250,150,70f,90f)
// draMyArc(canvas!!,PointF(300F,300F),250,150,135f,90f)
// draMyArc(canvas!!,PointF(300F,300F),250,150,225f,135f)
}
private fun draMyArc(
canvas: Canvas,
pointCenter: PointF,
radius: Float,
width: Float,
startAngle: Float,
sweepAngle: Float
) {
//添加大圆弧path
path.reset()
path.addArc(
pointCenter.x - radius, pointCenter.y - radius,
pointCenter.x + radius, pointCenter.y + radius, startAngle, sweepAngle
)
//计算出小圆弧起点
val endAngle = startAngle + sweepAngle.toDouble()
val smallR = radius - width
val smallRStartX = pointCenter.x + smallR * cos(Math.toRadians(endAngle))
val smallRStartY = pointCenter.y + smallR * sin(Math.toRadians(endAngle))
val smallREndX = pointCenter.x + smallR * cos(Math.toRadians(startAngle.toDouble()))
val smallREndY = pointCenter.y + smallR * sin(Math.toRadians(startAngle.toDouble()))
//连接小圆弧起点
path.lineTo(smallRStartX.toFloat(), smallRStartY.toFloat())
//添加小圆弧path
path.addArc(
pointCenter.x - smallR, pointCenter.y - smallR,
pointCenter.x + smallR, pointCenter.y + smallR, endAngle.toFloat(), -sweepAngle
)
//计算出大圆弧起点
val bigRStartX = pointCenter.x + radius * cos(Math.toRadians(startAngle.toDouble()))
val bigRStartY = pointCenter.y + radius * sin(Math.toRadians(startAngle.toDouble()))
//连接大圆弧起点
// path.lineTo(bigRStartX.toFloat(), bigRStartY.toFloat())
path.setLastPoint(smallREndX.toFloat(),smallREndY.toFloat())
path.close()
canvas?.drawPath(path, paint)
}
}