package com.abbidot.tracker.widget

import android.content.Context
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.tracker.R

/**
 *Created  by .yzq on 2022/4/25/025.
 * @link
 * @description:
 */
class MyView : View {

    private lateinit var mPaint: Paint
    private lateinit var mCamera: Camera

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs,
            defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {
        //禁用硬件加速,防止返回到界面多次执行onDraw
        setLayerType(LAYER_TYPE_SOFTWARE, null)

        //开启抗锯齿
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 2f
        mPaint.color = ContextCompat.getColor(context, R.color.gray_color3)

        mCamera= Camera()
    }


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

//        mCamera.save() // 保存 Camera 的状态
        mCamera.rotateX(60f) // 旋转 Camera 的三维空间
        canvas.translate(width/2f, height/2f) // 旋转之后把投影移动回来
        mCamera.applyToCanvas(canvas) // 把旋转投影到 Canvas
        canvas.translate(-width/2f, -height/2f) // 旋转之前把绘制内容移动到轴心（原点）
//        mCamera.restore() // 恢复 Camera 的状态


        canvas.drawCircle(width/2f, height/2f, 50f, mPaint)
        canvas.drawCircle(width/2f, height/2f, 150f, mPaint)
        mPaint.strokeWidth = 2f
        canvas.drawCircle(width/2f, height/2f, 250f, mPaint)
        mPaint.strokeWidth =2f
        canvas.drawCircle(width/2f, height/2f, 350f, mPaint)
    }
}