QQ计步

2019-12-11  本文已影响0人  淹死丶的鱼

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import kotlin.math.abs
/**
 *created by 淹死的鱼 on 2019/12/11
 */
class QQStepView @JvmOverloads constructor(
    context: Context,
    attSet: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attSet, defStyleAttr) {
    private var innerColor = 0
    private var outerColor = 0
    private var textColor = 0
    private var textSize = 0f
    private var borderWidth = 0f
    private val outPaint: Paint
    private val innerPaint: Paint
    private val textPaint: Paint
    //最大步数
    private var mStepMax = 0
    //当前步数
    private var mCurrentStep = 0

    // 分析效果
    //确定属性
    //布局中使用
    //获取自定义值
    init {
        val typedArray = context.obtainStyledAttributes(attSet, R.styleable.QQStepView)
        innerColor = typedArray.getColor(R.styleable.QQStepView_innerColor, innerColor)
        outerColor = typedArray.getColor(R.styleable.QQStepView_outerColor, outerColor)
        textColor = typedArray.getColor(R.styleable.QQStepView_stepTextColor, textColor)
        textSize = typedArray.getDimension(R.styleable.QQStepView_stepTextSize, textSize)
        borderWidth = typedArray.getDimension(R.styleable.QQStepView_borderWidth, borderWidth)
        typedArray.recycle()
        //外弧
        outPaint = Paint()
        outPaint.color = outerColor
        outPaint.isAntiAlias = true
        outPaint.strokeWidth = borderWidth
        outPaint.style = Paint.Style.STROKE
        outPaint.strokeCap = Paint.Cap.ROUND
        //内弧
        innerPaint = Paint()
        innerPaint.color = innerColor
        innerPaint.isAntiAlias = true
        innerPaint.strokeWidth = borderWidth
        innerPaint.style = Paint.Style.STROKE
        innerPaint.strokeCap = Paint.Cap.ROUND
        //文字
        textPaint = Paint()
        textPaint.color = textColor
        textPaint.isAntiAlias = true
        textPaint.textSize = textSize
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //布局可能是 warp_content
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        setMeasuredDimension(if (width > height) height else width, if (width > height) height else width)
        val center = width / 2f
        val radius = width / 2 - borderWidth / 2
        rectF.set(center - radius, center - radius, center + radius, center + radius)
    }

    private val rect = Rect()
    private var rectF = RectF()
    //画外圆弧 内圆弧 文字
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        //画外圆弧
        canvas.drawArc(rectF, 135f, 270f, false, outPaint)

        //画内圆弧
        if (mStepMax == 0) return
        val sweepAngle = mCurrentStep.toFloat() / mStepMax
        canvas.drawArc(rectF, 135f, sweepAngle * 270, false, innerPaint)

        //画文字
        //基线
        val metricsInt = textPaint.fontMetricsInt
        val dy = abs(metricsInt.top - metricsInt.bottom) / 2 - abs(metricsInt.bottom)
        val baseLine = height / 2f + dy

        textPaint.getTextBounds(mCurrentStep.toString(), 0, mCurrentStep.toString().length, rect)
        val dx = width.toFloat() / 2 - rect.width() / 2
        canvas.drawText(mCurrentStep.toString(), dx, baseLine, textPaint)
    }

    //设置动画
    @Synchronized
    fun setMaxStep(maxStep: Int) {
        this.mStepMax = maxStep
    }

    @Synchronized
    fun setCurrent(currentStep: Int) {
        this.mCurrentStep = currentStep
        invalidate()
    }
}
    <declare-styleable name="QQStepView">
        <attr name="outerColor" format="color" />
        <attr name="innerColor" format="color" />
        <attr name="borderWidth" format="dimension" />
        <attr name="stepTextSize" format="dimension" />
        <attr name="stepTextColor" format="color" />
    </declare-styleable>
上一篇 下一篇

猜你喜欢

热点阅读