AnimationUtils认识

来源:互联网 发布:linux 链接文件夹 编辑:程序博客网 时间:2024/06/04 18:22

在我的“图片浏览产品说明书” 文章中,曾使用如下方法:

myImageSwitch.setInAnimation(AnimationUtils.loadAnimation(getApplication(), R.anim.left_in));
查看源码,第一句写着:

Defines common utilities for working with animations.

那就来看看,里面都有哪些方法吧

1、public static longcurrentAnimationTimeMillis() {

    return SystemClock.uptimeMillis();}

Returns the current animation time in milliseconds. This time should be used when invoking setStartTime(long) 设置动画开始时间


这里的主角

2、public static Animation loadAnimation(Context context, @AnimRes int id)        throws NotFoundException {    XmlResourceParser parser = null;    try {        parser = context.getResources().getAnimation(id);        return createAnimationFromXml(context, parser);    } catch (XmlPullParserException ex) {        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +                Integer.toHexString(id));        rnf.initCause(ex);        throw rnf;    } catch (IOException ex) {        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +                Integer.toHexString(id));        rnf.initCause(ex);        throw rnf;    } finally {        if (parser != null) parser.close();    }}
该方法用于加载XML格式的动画配置文件

在里面接下来的方法里,就去解析XML文件,并转换成相对应类型的动画

  ............................

if (name.equals("set")) {    anim = new AnimationSet(c, attrs);    createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);} else if (name.equals("alpha")) {    anim = new AlphaAnimation(c, attrs);} else if (name.equals("scale")) {    anim = new ScaleAnimation(c, attrs);}  else if (name.equals("rotate")) {    anim = new RotateAnimation(c, attrs);}  else if (name.equals("translate")) {    anim = new TranslateAnimation(c, attrs);} else {    throw new RuntimeException("Unknown animation name: " + parser.getName());}
.................

最后返回 return anim;

3、注意,要涨粉啦

public static LayoutAnimationController loadLayoutAnimation(Context context, @AnimRes int id)        throws NotFoundException {    XmlResourceParser parser = null;    try {        parser = context.getResources().getAnimation(id);        return createLayoutAnimationFromXml(context, parser);    } catch (XmlPullParserException ex) {        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +                Integer.toHexString(id));        rnf.initCause(ex);        throw rnf;    } catch (IOException ex) {        NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +                Integer.toHexString(id));        rnf.initCause(ex);        throw rnf;    } finally {        if (parser != null) parser.close();    }}
在Android中,最简单的动画就是补间动画了。通过补间动画,可以对一个控件进行位移、缩放、旋转、改变透明度等动画。但是补间动画只能对一个控件使用,如果要对某一组控件播放一样的动画的话,可以考虑layout-animation。

layout-animation可由xml和代码两种方式配置:

首先看看xml方式:

在anim文件夹下,建立对某一组控件播放一样的动画,例如list_layout_anim文件,其内容为:

<?xml version="1.0" encoding="utf-8"?><layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"    android:delay="30%"    android:animationOrder="reverse"    android:animation="@anim/right_in"    />
而right_in 属于某一控件的动画效果,其代码为:

<?xml version="1.0" encoding="UTF-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="100%p"        android:toXDelta="0"        android:duration="500"/></set>

使用方式:

在布局文件中,某一个viewGroup添加属性:    android:layoutAnimation="@anim/list_anim_layout" 就行



<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:layoutAnimation="@anim/list_anim_layout"    tools:context="ly.com.animationdemo.MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/one"/></LinearLayout>

这样在加载布局的时候就会自动播放layout-animtion。

  • 代码配置

如果在xml中文件已经写好LayoutAnimation,可以使用AnimationUtils直接加载:

  1. AnimationUtils.loadLayoutAnimation(context, id)
另外JAVA代码方式也可以实现同样的效果:

  1.  //通过加载XML动画设置文件来创建一个Animation对象;
  2.    Animation animation=AnimationUtils.loadAnimation(this, R.anim.slide_right);   //得到一个LayoutAnimationController对象;
  3.    LayoutAnimationController controller = new LayoutAnimationController(animation);   //设置控件显示的顺序;
  4.    controller.setOrder(LayoutAnimationController.ORDER_REVERSE);   //设置控件显示间隔时间;
  5.    controller.setDelay(0.3);   //为ListView设置LayoutAnimationController属性;
  6.    listView.setLayoutAnimation(controller);
  7.    listView.startLayoutAnimation();

同时,我在查阅这方面知识的同时,看到网友的相关拓展,大概思想如下

LayoutAnimation默认只有三种顺序,即顺序逆序和随机,不能满足需求。去翻翻源码看它是怎么实现的,有没有提供方法自定义顺序?结果翻到了一个LayoutAnimationController#getTransformedIndex(AnimationParameters params)方法,返回值就是播放动画的顺序。并且这个方法是protected的,明显就是可由子类来扩展。

  1.     protected int getTransformedIndex(AnimationParameters params) {
  2.         if(getOrder() == ORDER_CUSTOM &amp;&amp; onIndexListener != null) {
  3.             return onIndexListener.onIndex(this, params.count, params.index);
  4.         } else {
  5.             return super.getTransformedIndex(params);
  6.         }
  7.     }
重写上述方法,从而打破固有的三种模式,通过复写getTransformedIndex方法,添加自定义顺序ORDER_CUSTOM,让callback自定义控件播放动画的顺序,即可以达到任何想要的效果。更多的扩展等着你

4、动画效果

public static Animation makeInAnimation(Context c, boolean fromLeft) {    Animation a;    if (fromLeft) {        a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_left);    } else {        a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_right);    }    a.setInterpolator(new DecelerateInterpolator());    a.setStartTime(currentAnimationTimeMillis());    return a;}
public static Animation makeOutAnimation(Context c, boolean toRight) {    Animation a;    if (toRight) {        a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_right);    } else {        a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_left);    }    a.setInterpolator(new AccelerateInterpolator());    a.setStartTime(currentAnimationTimeMillis());    return a;}
用法如下:
// 向右边移出
ll_first.setAnimation(AnimationUtils.makeOutAnimation(thistrue));
// 向右边移入
ll_second.setAnimation(AnimationUtils.makeInAnimation(thistrue));
 

还有一些加速器的方法,剩下的网友自行去查看源码就知道了。完毕,到此结束!!!!!!!!!!

0 0
原创粉丝点击