博客
关于我
学习Android动画小结
阅读量:785 次
发布时间:2019-03-25

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

今天我在学习如何在Android系统中使用内置的动画效果,让应用能够与用户之间产生更多的互动。回想起来,对Android动画的学习经历相当愉快,也颇感挑战性。以下是我在这一过程中学到的内容。

常见的动画类型

在Android应用开发中,动画或许是用户体验最直接的感受方式之一。根据我的理解,基本上有三大动画类型供我们选择:

  • Tween Animation(补间动画)

    Tweet动画是基于用户需求的动画效果,根据已经有的属性值在短时间内平滑过渡到目标值。这个类型包括以下四种常用的实现:

  • Frame Animation(帧动画)

    Frame动画是一个画面显示序列的概念,通常需要预先准备不同的帧。它的实现方式包括在drawable目录下创建不同图像,并通过animation-list标签在xml文件中嵌套多个<item>标签。

  • Property Animation(属性动画)

    Property动画是一种真正改变视图自身属性的动画效果,适合处理复杂的视图变化。在早期的Android版本中,我们需要使用nineoldandroids-2.4.0.jar来实现这些效果。

  • 动画的实现方式选择

    基于上述动画类型,每种动画都可以通过两种主要方式实现:一种是通过Xml文件配置,另一种是通过直接编写代码实现。

    方法一:通过Xml配置实现

    可以通过AnimationUtils.loadAnimation()工具加载预先定义的Xml文件,具体实现如下:

    方法二:通过纯代码实现

    对于开发者来说,代码实现可能更灵活。以下是几种动画的代码示例:

    ScaleAnimation scaleAnimation=new ScaleAnimation(    0.5f,    1.0f,    0.5f,    1.0f);scaleAnimation.setRepeatMode(TRIM_MEMORY_BACKGROUND);scaleAnimation.setDuration(1000);view.startAnimation(scaleAnimation);

    对于RotateAnimation

    RotateAnimation rotateAnimation = new RotateAnimation(    0f,    360f,    Animation.RELATIVE_TO_SELF,    0.5f,    Animation.RELATIVE_TO_SELF,    0.5f);rotateAnimation.setDuration(1000);view.startAnimation(rotateAnimation);

    对于TranslateAnimation

    TranslateAnimation translateAnimation= new TranslateAnimation(    Animation.RELATIVE_TO_SELF,    1.0f,    Animation.RELATIVE_TO_SELF,    0f,    Animation.RELATIVE_TO_SELF,    1.0f,    Animation.RELATIVE_TO_SELF,    0f);translateAnimation.setDuration(1000);view.startAnimation(translateAnimation);

    对于AlphaAnimation

    AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1.0f);alphaAnimation.setDuration(1000);view.startAnimation(alphaAnimation);

    帧动画与属性动画

    除了上述动画类型,帧动画和属性动画也是不容忽视的重要手段。

    帧动画

    帧动画需要预先准备一系列图片,具体实现如下:

    ...

    属性动画

    属性动画适合需要改变View自身属性的情况,典型实现如下:

    UI切换动画

    在Android中,UI切换可以通过定义进入和退出的动画资源文件来实现。例如,可以在res/anim目录下创建in.xmlout.xml文件,分别定义切换时的动画效果。

    自定义动画控制器

    对于如ListView或GridView这样的布局,能够添加动画效果可以极大地提升用户体验。可以通过LayoutAnimationController实现不同的动画效果。该类提供了以下常用常量:

    • ORDER_NORMAL:按顺序填充条目
    • ORDER_RANDOM:随机填充条目
    • ORDER_REVERSE:倒序填充条目

    具体实现如下:

    mlistView.setAdapter(adapter);LayoutAnimationController mLac= new LayoutAnimationController(    AnimationUtils.loadAnimation(this, R.anim.zoom_in));mLac.setOrder(LayoutAnimationController.ORDER_NORMAL);mlistView.setLayoutAnimation(mLac);mlistView.startLayoutAnimation();

    总结

    通过上述学习,我掌握了Android动画的多种实现方式,并能够根据项目需求选择合适的动画效果。动画的应用不仅能够提升用户体验,也能让应用更加生动有趣。

    结束今天的学习笔记。希望大家能在接下来的学习中收获更多的知识,离不开大家的支持。

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

    你可能感兴趣的文章
    php 特定时间段统计,jpgraph某个时间段的数据统计
    查看>>
    php 生成csv mac下乱码
    查看>>
    php 生成证书 签名及验签
    查看>>
    php 的rsa加密与解密
    查看>>
    PHP 的标准输入与输出
    查看>>
    php 笔记 (早前的,很乱)
    查看>>
    PHP 第一天
    查看>>
    Redis使用量暴增,快速定位有哪些大key在作怪
    查看>>
    php 结课作业答案,北语201803考试批次《PHP》(结课作业)1.pdf
    查看>>
    PHP 统计数据功能 有感
    查看>>
    SpringBoot处理JSON数据
    查看>>
    Redis使用基本套路
    查看>>
    php 解决项目中多个自动加载冲突问题
    查看>>
    PHP 设置调试工具XDebug PHPStorm IDE
    查看>>
    php 身份证号检测
    查看>>
    PHP 输入输出流合集
    查看>>
    PHP 过滤器(Filter)
    查看>>
    php 运算符and or && || 的详解
    查看>>
    php 返回html字符串长度限制,记一次js中和php中的字符串长度计算截取的终极问题和完美...
    查看>>
    php 阿里云oss 上传回调
    查看>>