本文总结常用属性方法等,详细学习可使用如下郭霖大神文章:

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动画总结的更多相关文章

  1. Android动画效果之自定义ViewGroup添加布局动画

    前言: 前面几篇文章介绍了补间动画.逐帧动画.属性动画,大部分都是针对View来实现的动画,那么该如何为了一个ViewGroup添加动画呢?今天结合自定义ViewGroup来学习一下布局动画.本文将通 ...

  2. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

  3. Android动画效果之初识Property Animation(属性动画)

    前言: 前面两篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画).Frame Animation(逐帧动画)Andr ...

  4. Android动画效果之Frame Animation(逐帧动画)

    前言: 上一篇介绍了Android的Tween Animation(补间动画) Android动画效果之Tween Animation(补间动画),今天来总结下Android的另外一种动画Frame ...

  5. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  6. Android动画

    [浅谈Android动画] 总共四种:Tween Animation变换动画.Frame Animation帧动画 Layout Animation布局动画.Property Animation 属性 ...

  7. Android动画学习(二)——Tween Animation

    前两天写过一篇Android动画学习的概述,大致的划分了下Android Animation的主要分类,没有看过的同学请移步:Android动画学习(一)——Android动画系统框架简介.今天接着来 ...

  8. Android动画学习(一)——Android动画系统框架简介

    2015-11-09补充:Drawable Animation极有可能是Frame Animation 这几天在找工作,面试的时候被问到了Android动画,之前完全没接触过这部分,直接给懵了,当然其 ...

  9. Android动画的理解

    基础知识 在我们开始讲Android动画这个知识点之前,我们了解下相应的基础知识点. Shape篇 一般用Shape定义的XML文件是存放在Drawable目录下,广泛应用于在Button.TextV ...

  10. Android动画之淡入淡出

    为了更好的说明Android动画的淡入淡出效果,这里以一个场景为例: 界面上有两个View 控件,两个View交替显示,当一个View淡入显示,另一个View淡出不可见. 我们把当前要显示的View叫 ...

随机推荐

  1. BugFree设置邮箱通知(这里以163邮箱为例)

    公司用bugfree在进行新建Bug指派抄送给同事的时候,总是有人不及时登录BugFree去查看指派给自己的,所以要加一个邮箱通知,这样可以及时通知到被指派的同事. 百度上很多用的是QQ邮箱来实现的, ...

  2. (一 ) 天猫精灵接入Home Assistant-服务器搭建

    ()1登陆阿里云 https://ecs.console.aliyun.com/?spm=5176.2020520001.0.0.6B1Uov#/home 账号dongdongmqtt 密码***** ...

  3. JavaScript高级程序设计学习(三)之变量、作用域和内存问题

    这次讲的主要是变量,作用域和内存问题. 任何一门编程语言,都涉及这三个. 变量,比如全局变量,局部变量等,作用域,也分全局作用域和方法作用域,内存问题,在java中就涉及到一个垃圾回收的问题,由于ja ...

  4. MATLAB的两种移位运算

    MATLAB的两种移位运算: 1)circshift矩阵移位 circshift:循环移位数组 语法:B = circshift(A,shiftize) 说明: B  = circshift(A,sh ...

  5. 如何应用ML的建议-下

    正则化与过拟合(highvariance)和欠拟合(highbias)的关系-部分(五) ML的诊断方法-部分(六) 如何采取下一步-部分(七) 部分(五) 从图中可以看出,正则化项可以用来影响模型函 ...

  6. Luogu3164 CQOI2014 和谐矩阵 异或高斯消元

    传送门 题意:给出$N,M$,试构造一个$N \times M$的非全$0$矩阵,其中所有格子都满足:它和它上下左右四个格子的权值之和为偶数.$N , M \leq 40$ 可以依据题目中的条件列出有 ...

  7. Ubuntu 安装google chrome

    sudo apt-get install google-chrome-stable /usr/bin/google-chrome-stable

  8. 第一章:模型层model layer -- Django从入门到精通系列教程

    该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. 题外话: Django的教程写到这里,就进入 ...

  9. eclipse取消自动输入提示

    在设置Eclipse自动提示后,按a-z都会显示提示,但是我们需要键入Enter才会输入,而默认的所有都键入,非常弱智,可采用下面方法设置. 1,先找到相关的插件: window -> show ...

  10. [译]通往 Java 函数式编程的捷径

    原文地址:An easier path to functional programming in Java 原文作者:Venkat Subramaniam 译文出自:掘金翻译计划 以声明式的思想在你的 ...