安卓属性动画总结

作者: 蓝不蓝编程 | 来源:发表于2019-07-22 18:01 被阅读1次

属性动画简要介绍

安卓属性动画分为四种:

  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)
    }

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)
    }

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. 渐变(淡出)

  • 效果图:


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

6. 串联,淡出淡入

  • 效果图:


  • 代码:
    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)
    }

7. 并联,移动并缩小

  • 效果图:


  • 代码:
    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)
    }

Demo源代码

https://gitee.com/cxyzy1/animationDemo

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

相关文章

  • 安卓属性动画总结

    属性动画简要介绍 安卓属性动画分为四种: 位置变化:TranslateAnimation 缩放: ScaleAni...

  • 安卓动画--属性动画

    属性动画类似于跟看电影一样,都是多张图片,一贞一贞展现出来。可以配置在xml里面,通过每贞图片停留时间长短,结合在...

  • 安卓属性动画

    安卓动画主要分3种:、和。 补间动画(Tween Animation,又叫视图动画):通过对场景里的对象不断做图像...

  • 安卓中基本的动画总结

    安卓有三种基本的动画: 1.帧动画2.补间动画3.属性动画(安卓3.0以后加入) 帧动画 帧动画是在drawabl...

  • Android动画初探

    安卓动画目前共分为三种动画逐帧动画、补间动画和属性动画。 一、逐帧动画(frame-by-frame animat...

  • 【总结】安卓动画

    本篇为对android动画机制的粗略总结,目的是方便查询而非讲解,所以代码部分的注释比较多,但是很多该配图的地方没...

  • Android源码学习-属性动画源码浅析

    介绍 安卓的动画有:View动画,帧动画和属性动画。其中View动画只是让View的影像发生变化,并不能真的改变V...

  • 安卓属性动画小技巧

    首先,最重要的一点也是务必要记住的一点 view 的 getX() getY()方法取得的是以父 view 为参考...

  • 动画之概述

    安卓动画主要由三类,分别是帧动画、补间动画、属性动画 帧动画:就是将一张张单独的图片连贯的进行播放 补间动画:主要...

  • 自定义属性动画框架

    通过本篇文章,你将会了解 安卓属性动画的基本架构 插值器和估值器在动画中的作用 手撸属性动画 设想一下,如果你是g...

网友评论

    本文标题:安卓属性动画总结

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