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

    你可能感兴趣的文章
    mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
    查看>>
    Mysql8 数据库安装及主从配置 | Spring Cloud 2
    查看>>
    mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
    查看>>
    MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
    查看>>
    MYSQL8.0以上忘记root密码
    查看>>
    Mysql8.0以上重置初始密码的方法
    查看>>
    mysql8.0新特性-自增变量的持久化
    查看>>
    Mysql8.0注意url变更写法
    查看>>
    Mysql8.0的特性
    查看>>
    MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
    查看>>
    MySQL8修改密码的方法
    查看>>
    Mysql8在Centos上安装后忘记root密码如何重新设置
    查看>>
    Mysql8在Windows上离线安装时忘记root密码
    查看>>
    MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
    查看>>
    mysql8的安装与卸载
    查看>>
    MySQL8,体验不一样的安装方式!
    查看>>
    MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
    查看>>
    Mysql: 对换(替换)两条记录的同一个字段值
    查看>>
    mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
    查看>>
    MYSQL:基础——3N范式的表结构设计
    查看>>