博客
关于我
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/

    你可能感兴趣的文章
    Prometheus 采集器使用详解
    查看>>
    Prometheus 黑盒监控实战
    查看>>
    prometheus+alertmanager+grafana监控部署教程
    查看>>
    Prometheus+Grafana构建智能化Kubernetes监控系统实战
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    Prometheus+SpringBoot应用监控全过程详解
    查看>>
    prometheus安装
    查看>>
    Prometheus实战教程:监控Kafka消息
    查看>>
    Prometheus实战教程:监控mysql数据库
    查看>>
    Prometheus实战教程:监控Nginx状态
    查看>>
    prometheus常用exporter下载地址大全
    查看>>
    Prometheus快速搭建与监控Linux系统实战
    查看>>
    prometheus报警与恢复告警的格式
    查看>>
    Pytorch中安装 torch_geometric 详细图文操作(全)
    查看>>
    prometheus监控docker容器实战
    查看>>
    Prometheus监控k8s集群使用邮箱和微信告警!
    查看>>
    Prometheus监控mysq数据库实战
    查看>>
    prometheus监控nginx实战
    查看>>
    Prometheus监控redis数据库实战
    查看>>
    Prometheus监控教程:使用Grafana展示主机基本信息
    查看>>