安卓属性动画总结

属性动画简要介绍

安卓属性动画分为四种:

  1. 位置变化:TranslateAnimation
  2. 缩放: ScaleAnimation
  3. 旋转: RotateAnimation
  4. 渐变: AlphaAnimation
    当然还可以将上面四种动画进行各种组合,可以采用串联或者并联实现多种效果.

动画实例

1. 位置变化

  • 效果图:
  • 代码:
       private fun move(srcView: View, destView: View) {
        val animateTime = 2000L
        val yOffset = destView.top - srcView.top
        val xOffset = destView.left - srcView.left
        val animation = TranslateAnimation(0f, xOffset.toFloat(), 0f, yOffset.toFloat())

        animation.fillAfter = true
        animation.duration = animateTime
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.clearAnimation()
                srcView.visibility = View.INVISIBLE
            }

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

        srcView.startAnimation(animation)
    }

2. 缩小

  • 效果图:
  • 代码:
    private fun scaleDown(srcView: View) {
        val animateTime = 2000L
        val animation = ScaleAnimation(1f, 0.2f, 1f, 0.2f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }
  • 从正中间缩小
    private fun scaleDown(srcView: View) {
        val animateTime = 2000L
        val animation = ScaleAnimation(1f, 0.2f, 1f, 0.2f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

3. 放大

  • 效果图:
  • 代码:
    private fun scaleDown(srcView: View) {
        val animateTime = 2000L
        val animation = ScaleAnimation(1f, 2f, 1f, 2f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }
  • 从正中间放大
private fun scaleDown(srcView: View) {
        val animateTime = 2000L
        val animation = ScaleAnimation(1f, 2f, 1f, 2f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

4. 旋转

  • 效果图:
  • 代码:
    private fun rotate(srcView: View) {
        val animateTime = 2000L
        val animation = RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

5. 风车(无限循环)

  • 效果图:
    image
  • 代码:
      private fun rotate(srcView: View) {
        val animateTime = 5000L
        val animation = RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation.fillAfter = true
        animation.repeatCount = Animation.INFINITE
        animation.duration = animateTime
        animation.interpolator = LinearInterpolator()
        srcView.startAnimation(animation)
    }

6. 渐变(淡出)

  • 效果图:
  • 代码:
    private fun fadeOut(srcView: View) {
        val animateTime = 2000L
        val animation =  AlphaAnimation(1f, 0f)
        animation.fillAfter = true
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

7. 串联,淡出淡入

  • 效果图:
  • 代码:
    private fun fadeOutAndFadeIn(srcView: View) {
        val animateTime = 2000L
        val fadeOutAnimation = AlphaAnimation(1f, 0f)
        fadeOutAnimation.fillAfter = true
        fadeOutAnimation.duration = animateTime

        val fadeInAnimation = AlphaAnimation(0f, 1f)
        fadeInAnimation.fillAfter = true
        fadeInAnimation.duration = animateTime

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

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

        srcView.startAnimation(fadeOutAnimation)
    }

PS: 如果希望两个动画中间存在停顿,可以给第二个动画加延时:设置startOffset属性即可.

8. 并联,移动并缩小

  • 效果图:
  • 代码:
    private fun scaleDownAndMove(srcView: View, destView: View) {
        val animateTime = 2000L
        val scaleAnimation = ScaleAnimation(1f, 0.2f, 1f, 0.2f,
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f)

        val yOffset = destView.top - srcView.top
        val xOffset = destView.left - srcView.left
        val translateAnimation = TranslateAnimation(0f, xOffset.toFloat(), 0f, yOffset.toFloat())//平移动画  从0,0,平移到100,100

        val animations = AnimationSet(false)
        animations.addAnimation(scaleAnimation)
        animations.addAnimation(translateAnimation)
        animations.fillAfter = true
        animations.duration = animateTime
        animations.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationEnd(animation: Animation) {
                srcView.clearAnimation()
                srcView.visibility = INVISIBLE
            }

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

        srcView.startAnimation(animations)
    }

PS:
如果要无限循环播放,添加如下代码:

 //设置无线循环
animation.repeatCount = Animation.INFINITE
 //设置匀速旋转
animation.interpolator = LinearInterpolator()

Demo源代码

https://gitee.com/cxyzy1/animationDemo

安卓开发技术分享: https://www.jianshu.com/p/442339952f26
点击关注专辑,查看最新技术分享
更多技术总结好文,请关注:「程序园中猿」

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值