Animations的使用(1)

什么是Animations

提供了一系列的动画效果,可以应用在绝大多数控件中

Animations的分类

1 Tweened Animations 渐变动画

提供了旋转,移动,伸展,淡出等效果

2 Frame-by-Frame Animations

可以创建一个Drawable序列,按照指定时间间歇一个个显示

Tweened Animations:

1 Alpha 淡入淡出效果

2 Scale 缩放效果

3 Rotate 旋转效果

4 Translate 移动效果

Animations的第一种使用方法(代码实现,xml实现)

使用Tweened Animations的步骤:

1 创建一个AnimationSet对象

AnimationSet animationSet=new AnimationSet(true);

2 根据需要创建相应的Animation对象(旋转,移动,伸展,淡出)

AlphaAnimation alphaAnimation = new AlphaAnimation(1,0); //参数为from..to..

*其他:

RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_PARENT(有3 种),1f,Animation.RELATIVE_TO_PARENT,0f);

3种坐标种类Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT,Animation.ABSOLUTE

ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

TranslateAnimation .......

3 根据软件动画的需求,为Animation对象设置相应数据

animationSet.setDuration(1000); //动画执行时间

4 将Animation对象添加到AnimationSet对象中

animationSet.addAnimation(alphaAnimation);

5 使用控件对象开始执行AnimationSet

imageView.startAnimation(animationSet);

Tweened Animations 通用属性

setDuration

setFillAfter

SetFillBefore

setStartOffSet

setRepeatCount

Animations使用(2)

接上篇

Animations的第二种使用方法(第一种见1)

步骤:

1 在res文件夹线面新建一个名为anim的文件夹

2 创建xml文件,并首先加入set标签,改标签如下

  1. <setxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:interpolator="@android:anim/accelerate_interpolator">
  3. ...
  4. </set>

3 在该标签中加入rotate,alpha,scale或者translate标签

例:

Alpha的alpha.xml文件编写方法(这些标签都是放在set标签中的)

  1. <alphaandroid:fromAlpha="1.0"
  2. android:toAlpha="0.0"
  3. android:atartOffset="500"
  4. android:duration="500"/>

rotate.xml

  1. <rotateandroid:fromDegrees="0"
  2. android:toDegrees="+350"         //正350度
  3. android:pivotX="50%"
  4. android:pivotY="50%"
  5. android:duration="3000"/>

这里要特别注意跟位置有关的参数pivotX和pivotY

3种写法对应3种相对位置方式的设置方式:

android:pivotX="50" 绝对定位

android:pivotX="50%" 相对于控件本身

android:pivotX="50%p" 相对于父控件

translate.xml

  1. <translateandroid:fromXDelta="50%"
  2. android:toXDelta="100%"
  3. android:fromYDelta="0%"
  4. android:toYDelta="100%"
  5. android:duration="1000"/>

scale.xml

  1. <scaleandroid:fromXScale="1.0"
  2. android:toXScale="0.0"
  3. android:fromYScale="1.0"
  4. android:toYScale="0.0"
  5. android:pivotX="50%"
  6. android:pivotY="50%"
  7. android:duration="2000"/>

4 在代码中使用AnimationUtils装载xml文件,并生成Animation对象

  1. Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);  //载入布局文件
  2. imageView.startAnimation(animation);

Animations的使用(3)

1 AnimationSet的使用方法

什么是AnimationSet

1 AnimationSet是Animation的子类

2 一个AnimationSet包含了一系列的Animation

3 针对AnimationSet设置一些Animation的常见属性(如StartOffset,duration等),可以被包含在AnimationSet当中的Animation继承

使用步骤:(类似1中的例子 只不过含有2个动画效果)

  1. AnimationSet animationSet = new AnimationSet(ture);
  2. AlpahaAnimation alpha = new AlphaAnimation(...);
  3. RotateAnimation rotate = new RotateAnimation(...);
  4. animationSet.addAnimation(alpha);
  5. animationSet.addAnimaion(rotate);
  6. animationSet.setDuration(2000);
  7. animationSet.setStartOffset(500);
  8. imageView.startAnimation(animationSet);

2 Interpolator的使用方法

Interpolator定义了动画变化速率,在Animations框架中定义了以下几种Interpolator

AccelerateDecelerateInterpolator:在动画开始和结束的地方速率变化较慢,中间的时候加速

AccelerateInterpolator:在动画开始的地方速率改变较慢,然后加速

CycleInterpolator:动画循环播放特定次数,速率改变沿正弦曲线

DecelerateInterpolator:在动画开始的地方速率改变较慢,然后减速

LinearInterpolator:以均匀的速率改变

设置的地方就在set标签中的 android:interpolator="@android:anim/accelerate_interpolator"

而之后还有一个android:shareInterpolator="true" 从名字就可以看到这是为set中所有的动画设置Interpolator

如果要单独设置 则将shareInterpolator设为false 然后为每个动画中单独定义Interpolator

以上是在xml中设置,如果要在代码中设置

animationSet.setInterpolator(new AccelerateInterpolator());(也可以单独设置)

注意在AnimationSet的构造方法中有一个boolean参数,这个参数就是shareInterpolator的设定

3 Frame-By-Frame Animations的使用方法

1 在res/drawable中创建一个xml文件,定义Animation的动画播放序列 anim_nv.xml

  1. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:oneshot="false">
  3. <itemandroid:drawable="@drawable/nv1"
  4. android:duration="500"/>
  5. <itemandroid:drawable="@drawable/nv2"
  6. android:duration="500"/>
  7. <itemandroid:drawable="@drawable/nv3"
  8. android:duration="500"/>
  9. <itemandroid:drawable="@drawable/nv4"
  10. android:duration="500"/>
  11. </animation-list>

2 为ImageView设置背景资源

  1. imageView.setBackgroundResource(R.drawable.anim_nv);

3 通过ImageView得到AnimationDrawable

  1. AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();

3 执行动画

  1. animationDrawable.start();

Animations使用(4)

LayoutAnimationController的使用方法(与ListView结合使用为例)

什么是LayoutAnimationController

1 LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果

2 每一个控件都有相同的动画效果

3 这些控件的动画效果在不同的时间显示出来

4 LayoutAnimationController可以在xml文件中设置,也可以在代码中设置

在XML中使用LayoutAnimaionController

1 在res/anim文件夹中创建一个文件,名为list_anim_layout.xml

  1. <layoutAnimationxmlns:android="http://schemas.android.com/apk/res/android"
  2. android:delay="0.5"
  3. android:animationOrder="random"
  4. android:animation="@anim/list_anim"/>

注意到list_anim这个xml文件,其中配置了动画效果,也就是一个动画配置文件(见3中)

<set>

<alpha...>

</set>

2 在布局文件中为ListView添加如下配置(就是在<listview>标签中添加一个属性)

android:layoutAnimation="@anim/list_anim_layout"

在代码中使用LayoutAnimationController

  1. 1 创建一个Animation对象:
  2. 可以通过装载xml,也可以直接使用Animation的构造函数创建Animation对象
  3. 2 创建LayoutAnimationController对象
  4. LayoutAnimationController lac=new LayoutAnimationController(animation);
  5. 3 设置控件显示顺序
  6. lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
  7. 4 为ListView设置LayoutAnimationController属性:
  8. listView.setLayoutAnimation(lac);

AnimationListener的使用方法

什么是AnimationListener

1 Animation是一个监听器

2 该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法

3 主要包含下面的三个方法

onAnimationEnd(Animation animation)

onAnimationRepeat(Animation animation)

onAnimationStart(Animation animation)

使用方法:

animation.setAnimationListener(new XxxAnimationListener);

其中XxxAnimationListener继承AnimationListene

在其中实现三个onXXX方法

Mars视频笔记——Animation的更多相关文章

  1. ng机器学习视频笔记(一)——线性回归、代价函数、梯度下降基础

    ng机器学习视频笔记(一) --线性回归.代价函数.梯度下降基础 (转载请附上本文链接--linhxx) 一.线性回归 线性回归是监督学习中的重要算法,其主要目的在于用一个函数表示一组数据,其中横轴是 ...

  2. ng机器学习视频笔记(二) ——梯度下降算法解释以及求解θ

    ng机器学习视频笔记(二) --梯度下降算法解释以及求解θ (转载请附上本文链接--linhxx)   一.解释梯度算法 梯度算法公式以及简化的代价函数图,如上图所示. 1)偏导数 由上图可知,在a点 ...

  3. ng机器学习视频笔记(十六) ——从图像处理谈机器学习项目流程

    ng机器学习视频笔记(十六) --从图像处理谈机器学习项目流程 (转载请附上本文链接--linhxx) 一.概述 这里简单讨论图像处理的机器学习过程,主要讨论的是机器学习的项目流程.采用的业务示例是O ...

  4. 斯坦福机器学习视频笔记 Week1 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  5. 慕课网,vue高仿饿了吗ASP源码视频笔记

    1.源码笔记 我的源码+笔记(很重要):http://pan.baidu.com/s/1geI4i2Z 感谢麦子学院项目相关视频 2.参考资料 Vue.js官网(https://vuejs.org.c ...

  6. 斯坦福机器学习视频笔记 Week1 线性回归和梯度下降 Linear Regression and Gradient Descent

    最近开始学习Coursera上的斯坦福机器学习视频,我是刚刚接触机器学习,对此比较感兴趣:准备将我的学习笔记写下来, 作为我每天学习的签到吧,也希望和各位朋友交流学习. 这一系列的博客,我会不定期的更 ...

  7. 无限互联IOS电影项目视频笔记

    下面是该iOS项目视频教程的内容大纲: 观看指南 (1)项目为第一阶段内容 (2)需要熟练掌握OC语言 (3)UI部分需要学习到第十节课 (4)项目适合刚入门的iOS开发者 1.第一天 (1)iOS ...

  8. 一名测试初学者听JAVA视频笔记(一)

    搭建pho开发环境与框架图 韩顺平 第一章: No1  关于文件以及文件夹的管理 将生成的文本文档做成详细信息的形式,显示文件修改时间以及文件大小,便于文件查看和管理,也是对于一名IT人士高效能工作的 ...

  9. angular2 学习笔记 ( animation 动画 )

    refer : https://angular.io/guide/animations https://github.com/angular/angular/blob/master/packages/ ...

随机推荐

  1. 第三方登录之QQ

    public class MainActivity extends AppCompatActivity { private Button btn; private TextView tv; priva ...

  2. JMeter定制Sampler

    1.背景 相信大家在使用JMeter工具测试的时候,经常会遇到自带采样器无法满足测试要求的情况.面对这种情况,通常的办法是使用万能的自定义Java Request的达到测试目的.这个方法有个弊端,只要 ...

  3. 关于js-xlsx的使用

    写在前头,本人是名Java开发人员,偶尔在前端打打酱油,写出的代码或许存在问题,请路过的大神一一指正,不吝感激. 最近公司准备做一些关于Excel 数据导入和导出相关需求,之前有在开源社区看到说比起纯 ...

  4. Jupter NotebooK学习

    1.参考资料 B站上学习视频 Jupyter 安装与使用 2.安装 在cmd窗口中输入(创建的文件会再当前的目录下):pip install jupyter 然后输入:jupyter notebook ...

  5. Example With JdbcDaoSupport

    By extended the JdbcDaoSupport, set the datasource and JdbcTemplate in your class is no longer requi ...

  6. springboot 整合shiro

    参考:        https://blog.csdn.net/fuweilian1/article/details/80309192(推荐)       https://blog.csdn.net ...

  7. 从技术小白到收获BAT研发offer,分享我的学习经验和感悟(赠送相关学习资料)

    去年秋季参加了校园招聘,有幸拿到了BAT.头条.网易.滴滴.亚马逊.华为等offer,经过研究生两年的学习积累,终于达成了自己的目标,期间也经历了很多,谨以此文,聊表感叹,也会分享很多我的Java学习 ...

  8. C++ 并发编程之互斥锁和条件变量的性能比较

    介绍 本文以最简单生产者消费者模型,通过运行程序,观察该进程的cpu使用率,来对比使用互斥锁 和 互斥锁+条件变量的性能比较. 本例子的生产者消费者模型,1个生产者,5个消费者. 生产者线程往队列里放 ...

  9. Mybatis基于注解实现多表查询

    对应的四种数据库表关系中存在四种关系:一对多,多对应,一对一,多对多.在前文中已经实现了xml配置方式实现表关系的查询,本文记录一下Mybatis怎么通过注解实现多表的查询,算是一个知识的补充. 同样 ...

  10. Libs - 颜色生成网站

    介绍几个免费常用的颜色生成网站: 如下 对比色邻近色配色方案 http://www.peise.net/tools/web/ 渐变色方案 https://webgradients.com/ 随机搭配5 ...