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

    你可能感兴趣的文章
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘html-webpack-plugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>