您当前的位置: 首页 >  动画

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

安卓动画样例-放大缩小

蓝不蓝编程 发布时间:2020-07-09 16:19:59 ,浏览量:0

效果图

实现代码
class CircleAnim(context: Context, attributeSet: AttributeSet? = null) :
    RelativeLayout(context, attributeSet) {

    init {
        addView(context, 300, "#009ad6")
        addView(context, 250, "#fab27b")
        addView(context, 200, "#f15b6c")
        addView(context, 150, "#6950a1")
        addView(context, 100, "#45b97c")
        scale(this)
    }

    private fun addView(context: Context, size: Int, colorStr: String): View {
        val view = View(context)
        view.background = getCircle(colorStr)
        val layoutParams = LayoutParams(size, size)
        layoutParams.addRule(CENTER_IN_PARENT)
        addView(view, layoutParams)
        return view
    }

    /**
     * 动画: 先放大,再缩小,再放大,一直循环
     */
    private fun scale(srcView: View) {
        val animateTime = 2000L

        val scaleDownAnimation = getScaleDownAnimation(animateTime)

        val scaleUpAnimation = getScaleUpAnimation(animateTime)

        scaleDownAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.startAnimation(scaleUpAnimation)
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        scaleUpAnimation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.startAnimation(scaleDownAnimation)
            }

            override fun onAnimationStart(animation: Animation) {}
            override fun onAnimationRepeat(animation: Animation) {}
        })

        srcView.startAnimation(scaleDownAnimation)
    }

    /**
     * 放大动画
     */
    private fun getScaleDownAnimation(animateTime: Long): ScaleAnimation {
        val scaleDownAnimation = ScaleAnimation(
            1f, 2f, 1f, 2f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )
        scaleDownAnimation.duration = animateTime
        return scaleDownAnimation
    }

    /**
     * 缩小动画
     */
    private fun getScaleUpAnimation(animateTime: Long): ScaleAnimation {
        val scaleUpAnimation = ScaleAnimation(
            2f, 1f, 2f, 1f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
        )
        scaleUpAnimation.duration = animateTime
        return scaleUpAnimation
    }

    /**
     * 获取空心圆
     */
    private fun getCircle(strokeColorStr: String) = GradientDrawable().also {
        it.shape = GradientDrawable.OVAL
        val strokeWidth = 2 // 边框宽度
        val strokeColor = Color.parseColor(strokeColorStr) //边框颜色
        it.setStroke(strokeWidth, strokeColor)
    }
}
安卓开发入门教程系列汇总 安卓发展历程及前景

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

初探安卓

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

开发语言学习

Kotlin语言基础

UI控件学习系列

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

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

关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0429s