如何在XML中使用自定义Animation动画类
来源:互联网 发布:手机安装监控软件 编辑:程序博客网 时间:2024/05/24 06:38
在安卓应用的动画开发中,可能SDK中自带的补间动画不能满足应用的需求,需要在Java代码中自定义一些动画类,当然都是继承自Animation类。实现之后,我们一般直接在代码中使用,类似下面这样:
1234
CustomAnimation customAnimation = new CustomAnimation();customAnimation.setDuration(3000);customAnimation.setFillAfter(true);effectView.startAnimation(customAnimation);
当View同时要应用像Scale,Alpha这样的补间动画时,你就需要多添加类似下面的代码:
1234567891011121314
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_set12345678910111213141516
<?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"><alphaandroid:fromAlpha="0"android:toAlpha="1"android:duration="400"/><myapp:cn.your.packagename.xx.CustomAnimationmyapp:customProp1="x" <!-- 动画类自定义属性1 -->myapp:customProp2="30" <!-- 动画类自定义属性2 -->myapp:customProp3="50%" <!-- 动画类自定义属性3 -->android:duration="400"android:fillAfter="true"/></set>
然后,我们按照常理来,在Java代码中这样来加载我们定义的xml动画集:
12
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.java1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
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。
转自:http://www.pedant.cn/2014/10/13/custom-animation-in-xml/
- 如何在XML中使用自定义Animation动画类
- 如何在xml中使用自定义的ViewGroup组件
- 如何在styles.xml中使用控件自定义属性
- Android中xml设置Animation动画
- android Animation动画的xml使用
- android Animation动画的xml使用
- android Animation动画的xml使用
- 在xml中使用属性动画
- 【Android 基础】Animation 动画介绍和实现【帧动画和Tween动画【在代码和xml中实现】】
- 在安卓中使用Animation类实现基础动画效果
- android 如何在XML中自定义属性
- 分别在XML文件、Java代码中实现动画效果 Animation(带示例)
- xml写Animation动画
- Android 自定义动画(Animation)
- Android自定义Animation动画
- Android自定义Animation动画
- 自定义ViewGroup、 动画Animation
- Android自定义Animation动画
- android开发小技巧(4)漂亮的单选按钮(非radiobutton)
- “网上订餐”缘何受到大学生青睐
- poj 3294 Life Forms (后缀数组应用)
- win7装ubuntu
- 玩玩PHP
- 如何在XML中使用自定义Animation动画类
- iOS——判断UITextField/UITextView输入的字符中是否含有系统表情
- ActivityManager框架解析
- iOS程序双击Home退出之后,SQL里的原来删除的东西又回来了?
- 【Astar】poj2449 Remmarguts' Date
- C#CookieHelper--C#操作Cookie的帮助类
- JAVA之URL
- LeetCode: Binary Search Tree Iterator
- Android 之 ServiceManager与服务管理