QQ计步

作者: 淹死丶的鱼 | 来源:发表于2019-12-11 17:07 被阅读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>

相关文章

  • QQ计步

  • 缘来如此

    百度QQ计步奖金赛报名参赛规则,偶然看到简书里一位朋友写的我正找的QQ计步文章的解答,让我彻底弄懂了QQ计...

  • 微信开发平台解密算法

    1、说明 小程序开发对接微信计步(https://developers.weixin.qq.com/minipro...

  • 12th January

    每日一句: 学一步,执行一步 健康状况: QQ计步?步 食用相思红豆粥,黄豆黑豆; 生姜泡脚X2; 香蕉; 牛奶;...

  • 8th January

    每日一句: 健康状况: QQ计步12725步 食用相思红豆粥,桂圆(一次少吃点),黄豆黑豆; 生姜泡脚; 茴香泡茶...

  • 15th January

    每日一句: 健康状况: QQ计步?步 用餐不及时〈早,中,晚〉:跟从群体,晚睡晚起 阅读写作: 完成英文写作 成就...

  • 10th January

    每日一句: 掌握临界知识,多多思考 健康状况: QQ计步?步 食用相思红豆粥,桂圆(一次少吃点),黄豆黑豆; 生姜...

  • 19th January

    每日一句: 健康状况: 1.QQ计步?步 阅读写作: 成就感: 提升点 控制不住的情绪 废话 感恩: 英语使用: ...

  • 居家,还思家

    因疫情,居家已经一月有余。 这段时间,走过最远的距离,便是从厨房到阳台。 “QQ运动”上显示“步数这么少,难道计步...

  • 9th January

    健康状况: QQ计步?步 食用相思红豆粥,桂圆(一次少吃点),黄豆黑豆; 生姜泡脚X2; 茴香泡茶; 阅读写作: ...

网友评论

      本文标题:QQ计步

      本文链接:https://www.haomeiwen.com/subject/elcagctx.html