如何在XML中使用自定义Animation动画类

在安卓应用的动画开发中,可能SDK中自带的补间动画不能满足应用的需求,需要在Java代码中自定义一些动画类,当然都是继承自Animation类。实现之后,我们一般直接在代码中使用,类似下面这样:

1
2
3
4
CustomAnimation customAnimation = new CustomAnimation();
customAnimation.setDuration(3000);
customAnimation.setFillAfter(true);
effectView.startAnimation(customAnimation);

当View同时要应用像Scale,Alpha这样的补间动画时,你就需要多添加类似下面的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CustomAnimation customAnimation = new CustomAnimation();
customAnimation.setDuration(3000);
customAnimation.setFillAfter(true);
Animation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Animation alphaAnimation = new AlphaAnimation(0.1f,1.0f);
scaleAnimation.setDuration(10000);
alphaAnimation.setDuration(10000);
AnimationSet set = new AnimationSet(true);
set.addAnimation(customAnimation);
set.addAnimation(scaleAnimation);
set.addAnimation(alphaAnimation);
set.setFillAfter(true);
set.setFillEnabled(true);
effectView.startAnimation(set);

如果直接在xml中把所需的所有补间动画,包括自定义动画类放到一个集合,事情看起来就没那么复杂。在xml定义好动画集有两个好处:

  • 使用动画时需要更少的Java代码,整体上看上去更干净
  • 在xml中定义,各个动画属性一目了然也更集中,方便阅读与维护

既然有这样的好处,我们就开始干吧。首先在xml中像下面这样定义一个动画集:

R.anim.my_anim_set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
android:interpolator="@android:anim/linear_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0"
android:toAlpha="1"
android:duration="400"/>
<myapp:cn.your.packagename.xx.CustomAnimation
myapp:customProp1="x" <!-- 动画类自定义属性1 -->
myapp:customProp2="30" <!-- 动画类自定义属性2 -->
myapp:customProp3="50%" <!-- 动画类自定义属性3 -->
android:duration="400"
android:fillAfter="true"/>
</set>

然后,我们按照常理来,在Java代码中这样来加载我们定义的xml动画集:

1
2
AnimationSet set = (AnimationSet)AnimationUtils.loadAnimation(this, R.anim.my_anim_set);
effectView.startAnimation(set);

但是,抱歉!上面的代码是不正确执行,运行起来程序会直接终止。那什么原因呢?查看AnimationUtils.loadAnimation源代码我们知道,在其从xml载入动画类的时候,只认alpha、scale、rotate、translate这几个SDK自带的动画类,而我们写入的自定义动画类CustomAnimation会导致其报Unknown animation name的异常。官方SDK也没有提供解决这个问题的其他API方法,那么怎么解决呢? 很简单,只需在原有的AnimationUtils.loadAnimation源码上改动一行,从ClassLoader载入自定义动画类即可。将其源码拷贝过来,实现一个自己的loadAnimation方法,如下:

OptAnimationLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public class OptAnimationLoader {
public static Animation loadAnimation(Context context, int id) throws Resources.NotFoundException {
XmlResourceParser parser = null;
try {
parser = context.getResources().getAnimation(id);
return createAnimationFromXml(context, parser);
} catch (XmlPullParserException ex) {
Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} catch (IOException ex) {
Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
rnf.initCause(ex);
throw rnf;
} finally {
if (parser != null) parser.close();
}
}
private static Animation createAnimationFromXml(Context c, XmlPullParser parser) throws XmlPullParserException, IOException {
return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
}
private static Animation createAnimationFromXml(Context c, XmlPullParser parser, AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
Animation anim = null;
// Make sure we are on a start tag.
int type;
int depth = parser.getDepth();
while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
if (type != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
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 {
try {
anim = (Animation) Class.forName(name).getConstructor(Context.class, AttributeSet.class).newInstance(c, attrs);
} catch (Exception te) {
throw new RuntimeException("Unknown animation name: " + parser.getName() + " error:" + te.getMessage());
}
}
if (parent != null) {
parent.addAnimation(anim);
}
}
return anim;
}
}

这样,使用OptAnimationLoader.loadAnimation方法就可以从xml中载入包含自定义动画的动画集了。

使用案例及更多demo见SweetAlertDialog

欢迎转载,请注明出处链接!!!