Android动画总结
本文总结常用属性方法等,详细学习可使用如下郭霖大神文章:
Android属性动画完全解析(上),初识属性动画的基本用法
Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
视图动画(View animation)
视图动画分两种子动画Tween animation和Frame animation,下面分别介绍他们的用法。
视图动画(View Animation)之帧动画(Frame Animation)
- 创建res/anim/sd_anim.xml
<?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/img_1"
android:duration="80" />
<item
android:drawable="@mipmap/img_2"
android:duration="80" />
<item
android:drawable="@mipmap/img_3"
android:duration="80" />
...
</animation-list>
- xml布局中
<ImageView
android:id="@+id/img_show"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="@anim/miao_gif" />
- 使用
ImageView img_show = (ImageView) findViewById(R.id.img_show);
AnimationDrawable anim = (AnimationDrawable) img_show.getBackground();
anim.start();//开始帧动画
anim.stop();//停止帧动画
视图动画(View Animation)之补间动画(Tween Animation)
AlphaAnimation(透明度渐变)
anim_alpha.xml:
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="2000"/>
属性解释:
- fromAlpha :起始透明度
- toAlpha:结束透明度
- 透明度的范围为:0-1,完全透明-完全不透明
ScaleAnimation(缩放渐变)
anim_scale.xml:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXScale="0.2"
android:toXScale="1.5"
android:fromYScale="0.2"
android:toYScale="1.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"/>
属性解释:
- fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
- toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
- pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
TranslateAnimation(位移渐变)
anim_translate.xml:
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="320"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="2000"/>
属性解释:
- fromXDelta/fromYDelta:动画起始位置的X/Y坐标
- toXDelta/toYDelta:动画结束位置的X/Y坐标
RotateAnimation(旋转渐变)
anim_rotate.xml:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="1"
android:repeatMode="reverse"/>
属性解释:
- fromDegrees/toDegrees:旋转的起始/结束角度
- repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
- repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!
AnimationSet(组合渐变)
anim_set.xml:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:shareInterpolator="true" >
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:duration="1000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
Android开发中必定会涉及到动画方面的效果,那么就会遇到一个问题,如果控制动画开始速度,与结束速度。还有其他的一些效果
控制动画速度(动画差值器)
在xml里面设置属性
android:interpolator="@android:anim/accelerate_interpolator" 设置动画为加速动画(动画播放中越来越快)
android:interpolator="@android:anim/decelerate_interpolator" 设置动画为减速动画(动画播放中越来越慢)
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 设置动画为先加速在减速(开始速度最快 逐渐减慢)
android:interpolator="@android:anim/anticipate_interpolator" 先反向执行一段,然后再加速反向回来(相当于我们弹簧,先反向压缩一小段,然后在加速弹出)
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段,然后加速反向回来,执行完毕自带回弹效果(更形象的弹簧效果)
android:interpolator="@android:anim/bounce_interpolator" 执行完毕之后会回弹跳跃几段(相当于我们高空掉下一颗皮球,到地面是会跳动几下)
android:interpolator="@android:anim/cycle_interpolator" 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)
android:interpolator="@android:anim/linear_interpolator" 线性均匀改变
android:interpolator="@android:anim/overshoot_interpolator" 加速执行,结束之后回弹
在代码中设置,顺序效果同上
animation.setInterpolator(new AccelerateInterpolator());
animation.setInterpolator(new DecelerateInterpolator());
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.setInterpolator(new AnticipateInterpolator());
animation.setInterpolator(new AnticipateOvershootInterpolator());
animation.setInterpolator(new BounceInterpolator());
animation.setInterpolator(new CycleInterpolator(2));
animation.setInterpolator(new LinearInterpolator());
animation.setInterpolator(new OvershootInterpolator());
动画不设置Interpolator属性即为默认值,匀速
Interpolator属性也可自定义
动画状态的监听
- setAnimationListener(new AnimationListener())方法,重写下面的三个方法:
- onAnimationStart():动画开始
- onAnimationRepeat():动画重复
- onAnimationEnd():动画结束
属性动画
Animator ————
———— AnimatorSet
———— ValueAnimator
———— ObjectAnimator
———— TimeAnimator
AnimatorSet:Animator 子类,用来组合动画
ObjectAnimator:ValueAnimator 子类,
ValueAnimator
- res/animator/***.xml 中定义ValueAnimator
语法
<animator
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"
android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
使用
//使用xml属性动画
ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.valueanimator);
//动画监听
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer animatedValue = (Integer) animation.getAnimatedValue();
view.scrollTo(animatedValue,0);
}
});
valueAnimator.start();
- Java属性介绍
//动画从0到10再到5再到0
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 10, 5, 0);
//ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 10, 5, 0);
/*
ofObject不同于ofFloat和ofInt,多了TypeEvaluator参数;其实ofInt和ofFloat使用的是系统默认的IntEvaluator和FloatEvaluator,而ofObject中参数TypeEvaluator需要我们自定义
*/
//ValueAnimator.ofObject(TypeEvaluator evaluator, Object... values);后文介绍
//动画中每一帧更新的时候调用
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
final float animatedValue = (float) animation.getAnimatedValue();
}
});
//动画的监听
valueAnimator.addListener(new AnimatorListenerAdapter() {
//动画取消
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
}
//动画结束
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
//动画重复
@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
//动画开始
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
}
//动画暂停
@Override
public void onAnimationPause(Animator animation) {
super.onAnimationPause(animation);
}
//动画重启
@Override
public void onAnimationResume(Animator animation) {
super.onAnimationResume(animation);
}
});
//动画执行时间
valueAnimator.setDuration(500);
//动画重复次数
valueAnimator.setRepeatCount(Integer.MAX_VALUE);
/*
动画循环模式,有两种取值:
ValueAnimator.RESTART:重新开始
ValueAnimator.REVERSE:反向重新开始
*/
valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
//动画差值器
valueAnimator.setInterpolator(new DecelerateInterpolator());
//设置动画延迟播放
valueAnimator.setStartDelay(1000);
//开始播放动画
valueAnimator.start();
- TypeEvaluator
ObjectAnimator
- 属性介绍
ObjectAnimator 使用与 唯一不同在于
/**
第二个参数 propertyName 说明:
alpha:透明度
rotation:旋转
rotationX:围绕x轴旋转
rotationY:围绕y轴旋转
translationX:在x轴平移
translationY:在y轴平移
scaleX:在x轴缩放
scaleY:在y轴缩放
backgroundColor:可以修改控件背景颜色
等等
*/
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 10, 10, 5, 1);
alphaAnimator.setDuration(2000);
alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
alphaAnimator.setRepeatCount(10);
alphaAnimator.setInterpolator(new DecelerateInterpolator());
alphaAnimator.start();
alphaAnimator.setStartDelay(1000);
了解更多 资源收集ResourceCollection
Android动画总结的更多相关文章
- Android动画效果之自定义ViewGroup添加布局动画
前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
- Android动画效果之初识Property Animation(属性动画)
前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...
- Android动画效果之Frame Animation(逐帧动画)
前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...
- Android动画效果之Tween Animation(补间动画)
前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...
- Android动画
[浅谈Android动画] 总共四种:Tween Animation变换动画.Frame Animation帧动画 Layout Animation布局动画.Property Animation 属性 ...
- Android动画学习(二)——Tween Animation
前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...
- Android动画学习(一)——Android动画系统框架简介
2015-11-09补充:Drawable Animation极有可能是Frame Animation 这几天在找工作,面试的时候被问到了Android动画,之前完全没接触过这部分,直接给懵了,当然其 ...
- Android动画的理解
基础知识 在我们开始讲Android动画这个知识点之前,我们了解下相应的基础知识点. Shape篇 一般用Shape定义的XML文件是存放在Drawable目录下,广泛应用于在Button.TextV ...
- Android动画之淡入淡出
为了更好的说明Android动画的淡入淡出效果,这里以一个场景为例: 界面上有两个View 控件,两个View交替显示,当一个View淡入显示,另一个View淡出不可见. 我们把当前要显示的View叫 ...
随机推荐
- Axios发送请求时params和data的区别
在使用axios时,注意到配置选项中包含params和data两者,以为他们是相同的,实则不然. 因为params是添加到url的请求字符串中的,用于get请求. 而data是添加到请求体(body) ...
- Properties集合_list方法与store方法
Properties集合和流对象结合的功能 list()方法: import java.util.Properties; public class PropertiesDemo { public st ...
- socket 总结
网络编程之进程:http://www.cnblogs.com/1a2a/p/7428759.html 网络编程之进阶:http://www.cnblogs.com/1a2a/p/7444446.htm ...
- Linux基础第五课——用户管理
用户管理 一个用户必须有一个主组 一个用户可以拥有多个组 但是必须一个主组 其它组是临时组 一个组可以拥有多个用户 用户的信息放到 /etc/passwd 用户的密码 存入 /etc/shadow 组 ...
- centos7下安装docker(3.2创建镜像build)
通过Dockerfile创建镜像 注:这个Dockerfile一开始真的不知道是在哪来的,还以为是在官网下载下来得(当然网上也有很多dockerfile的模板,参考:https://hub.docke ...
- JDK文档中关于Semaphore的正确使用以及使用场景
import java.util.concurrent.Semaphore; /** * * JDK文档使用备注:<br> * Semaphores are often used to r ...
- 左侧多级菜单,高亮显示js
左侧多级菜单,如果本页面是当前栏目,则左侧菜单高亮显示 <ul class="nav navbar-stacked" id="navs"> {ded ...
- php 对象数组互转
数组转对象 function array2object($array) { if (is_array($array)) { $obj = new StdClass(); forea ...
- Html5 标签三(图片)
1.图片标签 2.属性 一 图片标签 1.<img src="" alt=""> src可以是绝对路径.相对路径. 绝对路径:外部.内部(D:/Ht ...
- linux下打包压缩和解压命令
.tar 压缩:tar cvf FileName.tar FileName 解压:tar xvf FileName.tar .gz解压1:gunzip FileName.gz解压2:gzip -d F ...