博客
关于我
255_属性动画解析
阅读量:145 次
发布时间:2019-02-28

本文共 1116 字,大约阅读时间需要 3 分钟。

属性动画在Android开发中是一个强大的工具,能够通过代码实现丰富的视觉效果。以下将详细介绍如何使用ObjectAnimator类来实现属性动画。

属性动画的实现

属性动画的实现主要依赖于ObjectAnimator类。这类动画可以通过设置目标视图(View)的属性并提供初始值、目标值和动画时间来实现。例如,可以通过以下代码创建一个缩放Y轴的动画:

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "scaleY", 1f, 3f, 1f).setDuration(3000).start();

需要注意的是,ObjectAnimator支持的属性取决于目标View实现了哪些接口。例如,TextView类支持以下属性:

  • rotation: 旋转角度
  • translationX: X轴平移量
  • translationY: Y轴平移量
  • scaleX: X轴缩放比例
  • scaleY: Y轴缩放比例
  • alpha: 不透明度

属性动画的常用示例

以下是一些常用的属性动画示例:

  • 旋转动画
  • ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f).setDuration(3000).start();
    1. 平移动画
    2. float curTranslationX = textview.getTranslationX();ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "translationX", curTranslationX, -500f, curTranslationX).setDuration(3000).start();
      1. 缩放动画
      2. ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "scaleY", 1f, 3f, 1f).setDuration(3000).start();

        注意事项

        • 动画时间:动画时间设定在setDuration方法中,单位为毫秒。
        • 自动回放:默认情况下,动画会自动回放,除非调用cancel()方法。
        • 多个属性同时动画:可以通过多次调用ofFloat方法,针对不同的属性进行同时动画。

        总结

        属性动画是Android开发中非常强大的工具,通过简单的代码可以实现丰富的视觉效果。通过合理使用ObjectAnimator类,可以轻松创建旋转、缩放、平移等动画效果。希望以上示例能为您提供帮助!

    转载地址:http://obac.baihongyu.com/

    你可能感兴趣的文章
    Pinia:$subscribe()的使用场景
    查看>>
    Pinpoint对Kubernetes关键业务模块进行全链路监控
    查看>>
    Pinterest 大规模缓存集群的架构剖析
    查看>>
    pintos project (2) Project 1 Thread -Mission 1 Code
    查看>>
    PinYin4j库的使用
    查看>>
    PIP
    查看>>
    pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
    查看>>
    pip install mysqlclient报错
    查看>>
    pip install 出现报asciii码错误的解决
    查看>>
    pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
    查看>>
    pip 下载慢
    查看>>
    pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
    查看>>
    pip 安装opencv-python卡死
    查看>>
    pip 安装出现异常
    查看>>
    Pip 安装失败:需要 SSL
    查看>>
    Pip 安装挂起
    查看>>
    pip 或 pip3 为 Python 3 安装包?
    查看>>
    pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
    查看>>
    pip 无法从 requirements.txt 安装软件包
    查看>>
    pip/pip3更换国内源
    查看>>