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

    你可能感兴趣的文章
    Pinia入门(快速上手)
    查看>>
    Pinia:$patch的使用场景
    查看>>
    Pinia:$subscribe()的使用场景
    查看>>
    Pinpoint对Kubernetes关键业务模块进行全链路监控
    查看>>
    Pinterest 大规模缓存集群的架构剖析
    查看>>
    pintos project (2) Project 1 Thread -Mission 1 Code
    查看>>
    PinYin4j库的使用
    查看>>
    PIP
    查看>>
    pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
    查看>>
    pip install mysqlclient报错
    查看>>
    pip install 出现报asciii码错误的解决
    查看>>
    pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
    查看>>
    pip 下载慢
    查看>>
    pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
    查看>>
    pip 安装opencv-python卡死
    查看>>
    pip 安装出现异常
    查看>>
    Pip 安装失败:需要 SSL
    查看>>
    Pip 安装挂起
    查看>>
    pip 或 pip3 为 Python 3 安装包?
    查看>>
    pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
    查看>>