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叫 ...
随机推荐
- python中的轻量级定时任务调度库:schedule
提到定时任务调度的时候,相信很多人会想到芹菜celery,要么就写个脚本塞到crontab中.不过,一个小的定时脚本,要用celery的话太“重”了.所以,我找到了一个轻量级的定时任务调度的库:sch ...
- openssl交叉编译记录
本次任务是要完毕嵌入式Linux下对openssl程序的支持. 我的开发环境:ARM9开发板 和 嵌入式Linux操作系统.装有Linux系统(我的是ubuntu9.04)的PC机一台.串口和 ...
- 为什么express中打开服务端只用listen即可
为什么express中打开服务端只用listen即可:http.createServer(app).listen()与app.listen()的区别 写法一: var app = r ...
- (转)解决k8s集群提示docker login问题(同样适用于Rancher)
文章转自 https://blog.liv1020.com/ 参考文档:https://kubernetes.io/docs/concepts/containers/images/#configuri ...
- 按行读取.txt文件,并按行写入到新文件中
package com.test.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- cisco ospf
ospf配置:(Open Shortest Path First)PC0 and PC1 via Ospf to communicate. Network map as below.Environme ...
- STM32固件库详解
STM32固件库详解 emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...
- java 面向对象基本概念
1.面向对象与面向过程 (1).都是解决问题的思维方式,都是代码组织的方式. (2).解决简单问题可以使用面向过程 (3).解决复杂问题:宏观上使用面向对象把握,微观处理上仍然是面向过程. 2.面向对 ...
- Linux系列教程(四)——Linux常用命令之文件和目录处理命令
这个系列教程的前面我们讲解了如何安装Linux系统,以及学习Linux系统的一些方法.那么从这篇博客开始,我们就正式进入Linux命令的学习.学习命令,首先要跟大家纠正的一点就是,我们不需要记住每一条 ...