android 动画

摘要: 概述 最近总结一下Android的一些东西,毕竟基础不牢地动山摇。本篇主要涉及Animation,对Tween和Frame动画做些总结。

Tween Tween动画即补间动画,主要主持4种效果,缩放、透明度变化、旋转、平移,也可以组合起来使用(set/AnimationSet),在xml中对应的

概述

最近总结一下Android的一些东西,毕竟基础不牢地动山摇。本篇主要涉及Animation,对Tween和Frame动画做些总结。

Tween

Tween动画即补间动画,主要主持4种效果,缩放、透明度变化、旋转、平移,也可以组合起来使用(set/AnimationSet),在xml中对应的标签和Java 代码中体现如下

* scale —— ScaleAnimation
* alpha —— AlphaAnimation
* rotate —— RotateAnimation
* translate —— TranslateAnimation
* set —— AnimationSet

一般在xml文件中配置Tween动画,它们的公有属性如下:

* android:duration setDuration(long) 动画持续时间,以毫秒为单位
* android:fillAfter setFillAfter(boolean) 如果设置为true,控件动画结束时,将保持动画最后时的状态
* android:fillBefore setFillBefore(boolean) 如果设置为true,控件动画结束时,还原到开始动画前的状态
* android:fillEnabled setFillEnabled(boolean) 与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
* android:repeatCount setRepeatCount(int) 重复次数
* android:repeatMode setRepeatMode(int) 重复类型,有reverse和restart两个值,取值为RESTART或 REVERSE,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
* android:interpolator setInterpolator(Interpolator) 设定插值器,其实就是指定的动作效果,比如弹跳效果等

scale

scale标签是缩放动画,可以实现动态调控件尺寸的效果,有下面几个属性:

* android:fromXScale 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
* android:toXScale 结尾的X方向上相对自身的缩放比例,浮点值;
* android:fromYScale 起始的Y方向上相对自身的缩放比例,浮点值,
* android:toYScale 结尾的Y方向上相对自身的缩放比例,浮点值;
* android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,当为数值时,表示在当前View的左上角,即原点处加上50px,做为起始缩放点;如果是50%,表示在当前控件的左上角加上自己宽度的50%做为起始点;如果是50%p,那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。(具体意义,后面会举例演示)
* android:pivotY 缩放起点Y轴坐标,取值及意义跟android:pivotX一样。

放到代码中,ScaleAnimation有下面几个构造函数:

* ScaleAnimation(Context context, AttributeSet attrs)
* ScaleAnimation(float fromX, float toX, float fromY, float toY)
* ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
* ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

alpha

alpha标签主要用来管理透明度:

* android:fromAlpha 动画开始的透明度,从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明
* android:toAlpha 动画结束时的透明度,也是从0.0 –1.0 ,0.0表示全透明,1.0表示完全不透明

所对应的构造函数为:

* AlphaAnimation(Context context, AttributeSet attrs) 同样,从本地XML加载动画,基本不用
* AlphaAnimation(float fromAlpha, float toAlpha)

rotate

旋转标签

Rotate标签所具有的XML属性有:

* android:fromDegrees 开始旋转的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
* android:toDegrees 结束时旋转到的角度位置,正值代表顺时针方向度数,负值代码逆时针方向度数
* android:pivotX 缩放起点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
* android:pivotY 缩放起点Y轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p

对应的构造函数有:

* RotateAnimation(Context context, AttributeSet attrs)  从本地XML文档加载动画,同样,基本不用
* RotateAnimation(float fromDegrees, float toDegrees)
* RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
* RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

translate

平移标签

* android:fromXDelta 起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,比如 50、50%、50%p,具体意义已在scale标签中讲述,这里就不再重讲
* android:fromYDelta 起始点Y轴从标,可以是数值、百分数、百分数p 三种样式;
* android:toXDelta 结束点X轴坐标
* android:toYDelta 结束点Y轴坐标

这些属性所对应的构造函数为:

* TranslateAnimation(Context context, AttributeSet attrs) 同样,基本不用
* TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
* TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

插值器

用来定义动画速率如何变化

* AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
* AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
* AnticipateInterpolator 开始的时候向后然后向前甩
* AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值
* BounceInterpolator 动画结束的时候弹起
* CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
* DecelerateInterpolator 在动画开始的地方快然后慢
* LinearInterpolator 以常量速率改变
* OvershootInterpolator 向前甩一定值后再回到原来位置

使用

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true"
>
<alpha
android:fromAlpha="0.0" android:toAlpha="1.0"/> <scale
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4"/>
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720"/>
<translate
android:fromXDelta="0.0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="200"
android:toYDelta="200"/>
</set>
private ImageView ivShow;
private Animation setAnimation;
……
private void startSetnimation() {
setAnimation = AnimationUtils.loadAnimation(this, R.anim.setanimtion);
ivShow.startAnimation(setAnimation);
}

这里也对图片设置了点击监听,但是使用动画后的位置点击没有效果,但是点击原来位置的空白处仍有点击触发,这也体现了以前旧的动画的不足之处,就是属性并未改变,比如这里的监听,后面的属性动画正好解决了这一点。

Frame

其逐帧动画使用的还是比较多的,类似幻灯片方式来先编写好Drawable,然后代码中调用start()以及stop()开始或停止播放动画,当然我们也可以在Java代码中创建逐帧动画,关键标签为animation-list

<?xml version="1.0" encoding="utf-8"?><animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@mipmap/progress_1" android:duration="200"/>
<item android:drawable="@mipmap/progress_2" android:duration="200"/>
<item android:drawable="@mipmap/progress_3" android:duration="200"/>
<item android:drawable="@mipmap/progress_4" android:duration="200"/>
<item android:drawable="@mipmap/progress_5" android:duration="200"/>
<item android:drawable="@mipmap/progress_6" android:duration="200"/>
<item android:drawable="@mipmap/progress_7" android:duration="200"/>
<item android:drawable="@mipmap/progress_8" android:duration="60"/></animation-list>

通常在很多自定义的view中使用这种方式,比如加载动画,有一些的转圈圈就是一帧帧动画

这个给出一个customprogressDialog

/*

* 自定义旋转对话框

public class CustomProgressDialog extends Dialog {
private static CustomProgressDialog customProgressDialog = null; public CustomProgressDialog(Context context) {
super(context);
} public CustomProgressDialog(Context context, int theme) {
super(context, theme);
} public static CustomProgressDialog createDialog(Context context) {
customProgressDialog = new CustomProgressDialog(context,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
return customProgressDialog;
} public void onWindowFocusChanged(boolean hasFocus) {
if (customProgressDialog == null) {
return;
} ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
} public CustomProgressDialog setTitile(String strTitle) {
return customProgressDialog;
} public CustomProgressDialog setMessage(String strMessage) {
TextView tvMsg = (TextView) customProgressDialog
.findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null) {
tvMsg.setText(strMessage);
} return customProgressDialog;
}
}
```
其中customProgressDialog.setContentView(R.layout.customprogressdialog);的布局文件
```xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" > <ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/progress_round" /> <TextView
android:id="@+id/id_tv_loadingmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp" /> </LinearLayout>

imageView使用到了anim中的定义的帧动画

这个dialog的转圈就是通过8张图片通过帧动画来处理的。

代码传送门点击

参考:

http://blog.csdn.net/harvic880925/article/details/39996643

转载:http://blog.csdn.net/xsf50717/article/details/51144881

android动画基础之Animation的更多相关文章

  1. 公共技术点(Android 动画基础)

    转载地址:http://p.codekk.com/blogs/detail/559623d8d6459ae793499787 一 传统 View 动画(Tween/Frame) 1.1 Tween 动 ...

  2. android 动画基础绘——view 动画

    前言 对android 动画的整理,android 动画分为view动画(也叫补间动画),帧动画,属性动画. 看到这几个概念,让我想起了flash这东西.如果需要查各种动画具体的含义,那么可以去查询f ...

  3. Android 动画基础——视图动画(View Animation)

    本篇讲android 3.0之前被广泛的动画框架——ViewAnimation. 目录 我将分为六部分来讲: 概述 Alpha透明动画 Rotate旋转动画 Translate位移动画 Scale放缩 ...

  4. Android动画基础——属性动画(Property Animation)

    本篇涉及例子下载:Github 本篇讲android 3.0引入的属性动画框架,上篇写视图动画View Animation时就说过ViewAnimation的缺点,那就是动画作用的是view本身的视觉 ...

  5. 详解Android动画之Tween Animation

    前面讲了动画中的Frame动画,今天就来详细讲解一下Tween动画的使用. 同样,在开始实例演示之前,先引用官方文档中的一段话: Tween动画是操作某个控件让其展现出旋转.渐变.移动.缩放的这么一种 ...

  6. android 动画基础绘——view 动画(二)[补]

    前言 这个是对view 动画的补充,是一些view 动画的特殊使用场景. 回顾第一篇关于view 动画的,我介绍到view的动画都是针对元素本身的. 当我们开发view动画的时候,我们看到几个元素在做 ...

  7. 详解Android动画之Frame Animation(转)

    在开始实例讲解之前,先引用官方文档中的一段话: Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画.Frame动画可以被定义在XML文件中,也可以完全编码实现. ...

  8. 详解Android动画之Frame Animation

    在开始实例讲解之前,先引用官方文档中的一段话: Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画.Frame动画可以被定义在XML文件中,也可以完全编码实现. ...

  9. android 动画基础绘——帧动画(三)

    前言 这篇介绍帧动画. 什么是帧动画? 帧动画,非常好理解.就是轮播,比如我们看电视,其实就是一张一张播放过去的. 正文 <?xml version="1.0" encodi ...

随机推荐

  1. GET 和 POST 比较整理

    差异 上传文件只能使用 POST GET 传输数据有大小限制 GET 传输的数据类型不灵活:GET是使用url来传输数据,那么比如空格字符以及类似&这样的字符就不方便传输.(当然可以使用发送前 ...

  2. ViewPager使用记录2——展示动态数据

    ViewPager是v4支持库中的一个控件,相信几乎所有接触Android开发的人都对它不陌生.之所以还要在这里翻旧账,是因为我在最近的项目中有多个需求用到了它,觉得自己对它的认识不够深刻.我计划从最 ...

  3. MongoDB用户权限管理

    创建用户账号: (roles参数指定了用户的角色以及这个账号授权的数据库,在同一个数据库中不能同时创建两个用户名相同的账号) Mongodb内置的用户角色: 数据库用户角色:read.readWrit ...

  4. LeetCode 404. Sum of Left Leaves (左子叶之和)

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  5. MYSQL 子查询返回多列显示

    因工作需要,目前研究出一种mysql 技能,与大家分享一下. 需求:关联查询另一个大表数据的某些(一个以上)字段 方案:因关联查询的表数据太大.多表查询影响效率,单个子查询又有些多余.所以采用多列拼接 ...

  6. 老生常谈:关于undo表空间的使用率

    就在前几天,又有一个客户向我咨询undo表空间使用率的问题. 这让我想起几年前曾经有个省份的案例,客户的实际运维人员是一位刚毕业不久的女孩,几乎不懂Oracle原理,项目经理交给她的任务也是基础运维工 ...

  7. angular控制器、服务和指令三者之间的关系

    从总体来看,这三个组件的关系如下所示: 服务负责从远端服务器抓取和存储数据. 基于服务构建的控制器将为angular的作用域层次提供数据和功能. 基于服务和控制器构建的指令将直接与文档对象模型(DOM ...

  8. 网络基础四 DNS DHCP 路由 FTP

    第1章 网络基础 1.1 IP地址分类 IP地址的类别-按IP地址数值范围划分 IP地址的类别-按IP地址用途分类 IP地址的类别-按网络通信方式划分 1.2 局域网上网原理过程 DHCP原理过程详情 ...

  9. ⑨bootstrap组件 按钮式下拉菜单 输入框 使用基础案例

      <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...

  10. day2--计算机基础

    一.服务器 1u服务器,1u=4.45cm 戴尔服务器种类: 电脑主机组成 主板.CPU.内存.硬盘.显卡.声卡等等. 运维关注三大部件:CPU.内存.硬盘(Disk) 电源,考虑使用双电服务器,电源 ...