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

    你可能感兴趣的文章
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>