安卓开发

安卓动画样例-圆环变多变少

2020-07-09  本文已影响0人  蓝不蓝编程

效果图

代码

class CircleViewAnim(context: Context, attributeSet: AttributeSet? = null) :
    View(context, attributeSet) {
    private var mPaint: Paint = Paint()
    private var mTimer: Timer? = null
    private var basePadding = 20
    private var count = 0
    private var maxCount = 20
    private var isIncreasing = true

    init {
        mPaint.color = Color.parseColor("#fab27b")
        mPaint.isAntiAlias = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeWidth = 10f
        startAnim()
    }

    private fun drawCircle(canvas: Canvas) {
        (0..count).forEach {
            var padding = it * basePadding
            canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), 10f + padding, mPaint)
        }
        if (isIncreasing) {
            count++
        } else {
            count--
        }
        if (count >= maxCount) {
            isIncreasing = false
        } else if (count <= 0) {
            isIncreasing = true
        }
    }

    private fun startAnim() {
        mTimer = Timer()
        mTimer?.schedule(object : TimerTask() {
            override fun run() {
                postInvalidate()
            }
        }, 50, 50)
    }

    override fun onDraw(canvas: Canvas) {
        drawCircle(canvas)
    }
}

安卓开发入门教程系列汇总

安卓发展历程及前景

安卓发展历程
安卓开发前景展望

初探安卓

安装开发工具
创建第一个安卓工程

开发语言学习

Kotlin语言基础

UI控件学习系列

UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar

关注头条号,第一时间获取最新文章:


上一篇 下一篇

猜你喜欢

热点阅读