/** * 作者:David Zheng on 2015/11/7 15:38 * * 
网站:http://www.93sec.cc * *
 微博:http://weibo.com/mcxiaobing * * 
微博:http://weibo.com/93sec.cc */ 个人交流QQ986945193

一、分类:

(一)、概要:

        3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android3.0中又引入了一个新的动画系统:属性动画(property animation)。
        这三种动画模式在SDK中被称为view animation,drawable animation,property animation。

(二)、动画资源分类:

  1. 属性动画:Property Animation
  2. 帧动画:Frame Animation (Drawable Animation)
  3. 补间动画:Tween Animation (View Animation)
    • 透明度补间动画
    • 缩放补间动画
    • 旋转补间动画
    • 移动补间动画

二、补间动画:
        View Animation就是一系列View形状的变换,如大小的缩放、透明度的改变、位置的改变、旋转位置改变,动画的定义既可以用java代码定义也可以用XML定义。建议用XML定义。
        用XML定义的动画放在/res/anim/文件夹内,XML文件的根元素为<set> , 二级节点可为<alpha>,<scale>,<translate>,<rotate>。
(一)、用xml资源实现补间动画:

(二)、用java代码实现补间动画:

public class MainActivity extends Activity {

private ImageView imageView_main;

private Animation animation = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}


public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:
animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
break;
case R.id.button_main_translate:
animation = new TranslateAnimation(0, 150, 0, 0);
break;
case R.id.button_main_rotate:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
default:
break;
}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}


三、帧动画:
        Frame Animation(AnimationDrawable对象):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。
        必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。XML文件要放在/res/anim/或者/res/animator或者/res/drawable目录下。
(一)、实例代码:
一、res/anim/frame_animation.xml的代码:

<animation-listxmlns:android="http://schemas.android.com/apk/res/android"

android:oneshot="true">

<itemandroid:drawable="@drawable/anim1"android:duration="50"/>

<itemandroid:drawable="@drawable/anim2"android:duration="50"/>

<itemandroid:drawable="@drawable/anim3"android:duration="50"/>

<itemandroid:drawable="@drawable/anim4"android:duration="50"/>

<itemandroid:drawable="@drawable/anim5"android:duration="50"/>

<itemandroid:drawable="@drawable/anim6"android:duration="50"/>

<itemandroid:drawable="@drawable/anim7"android:duration="50"/>

<itemandroid:drawable="@drawable/anim8"android:duration="50"/>

<itemandroid:drawable="@drawable/anim9"android:duration="50"/>

<itemandroid:drawable="@drawable/anim10"android:duration="50"/>

<itemandroid:drawable="@drawable/anim11"android:duration="50"/>

<itemandroid:drawable="@drawable/anim12"android:duration="50"/>

</animation-list>

二、MainActivity.java代码:
public class MainActivity extends Activity {

private ImageView imageView_main_show;

private AnimationDrawable animationDrawable = null;

@Override

protected void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView_main_show = (ImageView) findViewById(R.id.imageView_main_show);

imageView_main_show.setBackgroundResource(R.anim.frame_animation);

animationDrawable = (AnimationDrawable) imageView_main_show.getBackground();

}

public void clickButton(View
view) {

switch (view.getId()) {

case R.id.button_main_start:

if (!animationDrawable.isRunning()) {

//一组动画是否只播放一次

animationDrawable.setOneShot(false);

animationDrawable.start();

}

break;

case R.id.button_main_stop:

if (animationDrawable.isRunning()) {

animationDrawable.stop();

}

break;

}

}

@Override

public void onWindowFocusChanged(boolean hasFocus)
{

super.onWindowFocusChanged(hasFocus);

if (!animationDrawable.isRunning()) {

animationDrawable.setOneShot(false);

animationDrawable.start();

}

}

}


【备注:】
        SDK中提到,不要在onCreate()中调用start(),因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。


四、属性动画:
(一)、概念:
        属性动画,这个是在Android 3.0中才引进的。Property Animation故名思议就是通过动画的方式改变对象的属性.属性动画更改的是对象的实际属性,在View Animation(Tween Animation)中,其改变的是View的绘制效果,真正的View的属性保持不变。
        比如无论如何缩放Button的大小,Button的有效点击区域还是没有应用动画时的区域,其位置与大小都不变。而在Property Animation中,改变的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。
        Property Animation不止可以应用于View,还可以应用于任何对象。Property Animation只是表示一个值在一段时间内的改变,当值改变时要做什么事情完全是你自己决定的。

(二)、常用属性:
  1. Duration动画的持续时间,默认300ms。
  2. Time interpolation:时间插值。LinearInterpolator、AccelerateDecelerateInterpolator,定义动画的变化率。
  3. Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。
  4. Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
  5. Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。
(三)、相关的类:
  1. ObjectAnimator 动画的执行类(常用属性:alpha,rotation,rotationX,rotationY,translationX,translationY,scaleX,scaleY)
  2. ValueAnimator 动画的执行类
  3. AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
  4. AnimatorInflater 用户加载属性动画的xml文件
  5. TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
  6. TimeInterpolator 时间插值
  • 总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。

(一)、实例代码:
一、res/anim/property_anim.xml的代码:

<setxmlns:android="http://schemas.android.com/apk/res/android"

android:ordering="sequentially">

<objectAnimator

android:duration="4000"

android:propertyName="x"

android:valueTo="300"

android:valueType="intType"/>

<objectAnimator

android:duration="4000"

android:propertyName="y"

android:valueTo="400"

android:valueType="intType"/>

<objectAnimator

android:duration="4000"

android:propertyName="x"

android:valueTo="0"

android:valueType="intType"/>

<objectAnimator

android:duration="4000"

android:propertyName="y"

android:valueTo="0"

android:valueType="intType"/>

</set>

二、MainActivity.java代码:

public class MainActivity extends Activity
{

private ImageView imageView_main_obj;

private Move move;

@Override

public void onCreate(Bundle
savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView_main_obj = (ImageView) findViewById(R.id.imageView_main_obj);

move = new Move();

imageView_main_obj.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v)
{

setTitle(move.getX() + ":" + move.getY());

}

});

}

class Move {

private int y;

private int x;

publicint getY() {

return y;

}

public void setY(int y)
{

this.y = y;

imageView_main_obj.layout(imageView_main_obj.getLeft(), y,imageView_main_obj.getRight(),y
+ imageView_main_obj.getMeasuredHeight());

}

public int getX() {

return x;

}

publi cvoid setX(int x)
{

this.x = x;

imageView_main_obj.layout(x, imageView_main_obj.getTop(), x + imageView_main_obj.getMeasuredWidth(),imageView_main_obj.getBottom());

}

}

public void clickButton(View
view) {

// 装载属性动画资源

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this,

R.animator.property_anim);

// 设置要控制的对象

set.setTarget(move);

// 开始动画

set.start();

}

}

【备注说明:】


<objectAnimator

android:duration="4000"

android:propertyName="x"

android:valueTo="300"

android:valueType="intType"/>

【备注:】
  • android:ordering说明一系列动画动作的执行顺序,有两个选择: sequentially 和together,顺序执行还是一起执行;
  • objectAnimator 是设定动画实施的对象;
  • duration是该动画动作执行从开始到结束所用的时间;
  • android:repeatCount="infinite"   可以是整数或者infinite
  • android:repeatMode="restart"    可以是restart 或者 reverse
  • android:valueFrom=" "     整数|浮点数|颜色
/** * 作者:David Zheng on
2015/11/7 15:38 * * 

网站:http://www.93sec.cc
* *

 微博:http://weibo.com/mcxiaobing
* * 

微博:http://weibo.com/93sec.cc
*/ 个人交流QQ986945193

android开发之动画的详解 整理资料 Android开发程序小冰整理的更多相关文章

  1. Android Animations 视图动画使用详解!!!

    转自:http://www.open-open.com/lib/view/open1335777066015.html Android Animations 视图动画使用详解 一.动画类型 Andro ...

  2. Android安卓书籍推荐《Android驱动开发与移植实战详解》下载

    百度云下载地址:点我 Android凭借其开源性.优异的用户体验和极为方便的开发方式,赢得了广大用户和开发者的青睐,目前已经发展成为市场占有率很高的智能手机操作系统. <Android驱动开发与 ...

  3. 动画_ _ Android应用开发之所有动画使用详解

    转载: http://blog.csdn.net/yanbober/article/details/46481171 题外话:有段时间没有更新博客了,这篇文章也是之前写了一半一直放在草稿箱,今天抽空把 ...

  4. Android应用开发之所有动画使用详解

    题外话:有段时间没有更新博客了,这篇文章也是之前写了一半一直放在草稿箱,今天抽空把剩余的补上的.消失的这段时间真的好忙,节奏一下子有些适应不过来,早晨七点四十就得醒来,晚上九点四十才准备下班,好像最近 ...

  5. Android高效率编码-第三方SDK详解系列(二)——Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能

    Android高效率编码-第三方SDK详解系列(二)--Bmob后端云开发,实现登录注册,更改资料,修改密码,邮箱验证,上传,下载,推送消息,缩略图加载等功能 我的本意是第二篇写Mob的shareSD ...

  6. Android基础夯实--重温动画(五)之属性动画 ObjectAnimator详解

    只有一种真正的英雄主义 一.摘要 ObjectAnimator是ValueAnimator的子类,它和ValueAnimator一样,同样具有计算属性值的功能,但对比ValueAnimator,它会更 ...

  7. Android高效率编码-第三方SDK详解系列(一)——百度地图,绘制,覆盖物,导航,定位,细腻分解!

    Android高效率编码-第三方SDK详解系列(一)--百度地图,绘制,覆盖物,导航,定位,细腻分解! 这是一个系列,但是我也不确定具体会更新多少期,最近很忙,主要还是效率的问题,所以一些有效的东西还 ...

  8. Android Design Support Library使用详解

    Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...

  9. [Android新手区] SQLite 操作详解--SQL语法

    该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法  :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...

随机推荐

  1. charles抓取HTTPS设置,详细踩坑版

    写这篇文章的背景就是,每次我在一台新电脑上用charles抓包时,总是因为各种原因无法抓到https请求,每个百度出来的回答又不是那么详细,需要通过几篇回答才能解决过程中的各种问题,所以把自己的安装经 ...

  2. 关于welcom-file-list 失效

    遇到个很奇怪的问题: 在使用shrio的时候,未登陆的情况下,能正常识别转发welcome-file-list    index 登陆之后无法识别welcom-file-list,需要手动输入/ind ...

  3. Python 图像处理 OpenCV (16):图像直方图

    前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...

  4. 解决IIS发布时CS0016未能写入输出文件错误

    今天遇到一个将asp.net项目部署到IIS后访问的时候报的一个错误: 在网上查询了相关资料后,解决方法如下: 找到C:\Windows\下的temp文件,右键属性>安全>编辑,给其中II ...

  5. asp.net 远程模型验证

    有这样一些场景,我们需要模型验证,某些字段不允许重复,但是又不希望在数据访问层增加一堆额外逻辑判断.我们需要数据访问层简洁,这种模型验证在进去Action之前,验证不通过直接告诉前端. 一个特性,继承 ...

  6. ASP.NET Core Logging Solution

    Serilog.Extensions.Logging.File This package makes it a one-liner - loggerFactory.AddFile() - to con ...

  7. elasticsearch 高级搜索示例 es7.0

    基础数据 创建索引 PUT mytest { "mappings": { "properties": { "title": { " ...

  8. Ant Design Pro入门教程,安装,运行(V5 Typescript版)

    [前言] 找了很多Admin模板,最后还是看中了AntDesignPro这个阿里巴巴开源的Admin框架,长这样(还行吧,目前挺主流的): 官网地址:https://pro.ant.design/in ...

  9. Java对象与类—对象与类

    1.类 类(class)是构造对象的模板,具体点说类就是对具有相同性质,相同行为的一群对象的抽象说明.由类构造(construst)对象的过程称为创建类的实例(instance). 2.对象 对象是类 ...

  10. 【JavaScript】windows.open用法详解

    windows.open("URL","窗口名称","窗口外观设定");的用法详解 function onNewWindows(redire ...