Android -- Fragment动画异常Unknown animation name: objectAnimator
异常
Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator
异常代码
FragmentTransaction ft = getFragmentManager().beginTransaction();
//setCustomAnimations()必须位于replace()之前,否则效果不起所中。它的两个参数分别为enter,exit的效果。系统目前提供两个效果,分别为android.R.animator.fade_in和android.R.animator.fade_out
ft.setCustomAnimations(R.animator.slide_in_left,R.animator.slide_out_right);
ft.addToBackStack(null);
ft.replace(R.id.details,"detail");
ft.commit();
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:valueFrom="-1280"
android:valueTo="0"
android:valueType="floatType"
android:propertyName="X"
android:duration="2000" />
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:valueFrom="0"
android:valueTo="1280"
android:valueType="floatType"
android:propertyName="X"
android:duration="2000" />
动画简单说明
实现自定义动画的类是ObjectAnimator,不仅用于fragment,也可用于view。在xml中,定义了从“from”状态到“to”状态,时间间隔为duration(毫秒),所执行的变化规则称为interpolator。最简单的interpolator是linear,即@android:interpolator/linear,从状态From到to状态是均匀变化。缺省的interpolator是accelerate_decelerate。系统提供的方式可以在源代码/data/res/interpolator中查看。android:propertyName用于动画的维度,在本例中X表示横向,根view的setX()中的参数是float,所以设置valueType为floatType。我们设置可以设置自己的维度。From设置为-1280,因为这个值对于终端设备而言,-1280个像素位可以确保从不可视的位置移入。如果我们没有设置From,系统会根据当前值来设定初始值。
如果我们要在两个或者两个以上的维度设置变化,可以使用set tag,对应为Android的AnimatorSet类,下面的例子同时设置向下和淡出效果。set有一个属性android:ordering,缺省为together,即各个维度的变化同时发生,还可以设置为sequentially依次发生。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<objectAnimator android:interpolator="@android:interpolator/accelerate_cubic"
android:valueFrom="1"
android:valueTo="0"
android:valueType="floatType"
android:propertyName="alpha"
android:duration="1000"/>
<objectAnimator android:interpolator="@android:interpolator/accelerate_cubic"
android:valueFrom="0"
android:valueTo="1280"
android:valueType="floatType"
android:propertyName="Y"
android:duration="1000"/>
</set>
异常分析
V4包中的Fragment对于动画的支持不完全。
在FragmentManager类中的loadAnimation方法
if (transitionStyle == 0) {
return null;
}
//TypedArray attrs = mActivity.obtainStyledAttributes(transitionStyle,
// com.android.internal.R.styleable.FragmentAnimation);
//int anim = attrs.getResourceId(styleIndex, 0);
//attrs.recycle();
//if (anim == 0) {
// return null;
//}
//return AnimatorInflater.loadAnimator(mActivity, anim);
return null;
在AnimatorInflater.loadAnimator里面处理的动画:
String name = parser.getName();
if (name.equals("objectAnimator")) {
anim = loadObjectAnimator(c, attrs);
} else if (name.equals("animator")) {
anim = loadAnimator(c, attrs, null);
} else if (name.equals("set")) {
anim = new AnimatorSet();
TypedArray a = c.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.AnimatorSet);
int ordering = a.getInt(com.android.internal.R.styleable.AnimatorSet_ordering,
TOGETHER);
createAnimatorFromXml(c, parser, attrs, (AnimatorSet) anim, ordering);
a.recycle();
} else {
throw new RuntimeException("Unknown animator name: " + parser.getName());
}
private static ObjectAnimator loadObjectAnimator(Context context, AttributeSet attrs)
throws NotFoundException { ObjectAnimator anim = new ObjectAnimator(); loadAnimator(context, attrs, anim); TypedArray a =
context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.PropertyAnimator); String propertyName = a.getString(com.android.internal.R.styleable.PropertyAnimator_propertyName); anim.setPropertyName(propertyName); a.recycle(); return anim;
}
So
在使用V4包中Fragment时,使用的切换动画效果,其动画文件中不能包含objectAnimator,Animator这类标签。如果必须要使用,请将工程中使用的V4包中Fragment相关类,换成源码中的Fragment相关类。
我是天王盖地虎的分割线
Android -- Fragment动画异常Unknown animation name: objectAnimator的更多相关文章
- Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常
在为Fragment做切换动画.启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAni ...
- Android 属性动画(Property Animation) 全然解析 (下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...
- Android 属性动画(Property Animation) 完全解析 (下)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...
- 通过AnimationSet 同步或一部播放多个动画 Android 属性动画(Property Animation) 完全解析 (下)
AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等. 以下例子同时应用5个动画: 播放anim1: 同时播放anim2,anim3,a ...
- android:Fragment动画的东西
最近很多人来Fragment动画是很感兴趣,我将是一个样本给大家看. 既然做,我会做动画以下类型: 注入弹出动画:从""进入.从"上下左右"弹出,当然,你怎么组 ...
- Android 属性动画(Property Animation) 完全解析 (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提 供了几种动画类型:View Anima ...
- 【转】Android 属性动画(Property Animation) 完全解析 (上)
http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提供了几种动画类型:View Animation .Dra ...
- Android 属性动画(Property Animation) 全然解析 (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提供了几种动画类型:View Animat ...
- 我的Android进阶之旅------>Android之动画之Frame Animation实例
============================首先看看官网上关于Frame animation的介绍================================ 地址:http://de ...
随机推荐
- Spring @RequestMapping 注解使用技巧
@RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...
- Codeforces Round #357 (Div. 2) A. A Good Contest 水题
A. A Good Contest 题目连接: http://www.codeforces.com/contest/681/problem/A Description Codeforces user' ...
- Codeforces Beta Round #97 (Div. 1) C. Zero-One 数学
C. Zero-One 题目连接: http://codeforces.com/contest/135/problem/C Description Little Petya very much lik ...
- hdoj 4445 Crazy Tank 物理题/枚举角度1
Crazy TankTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- .NET面试宝典-高级(一)
1. DateTime.Parse(myString); 这段代码有什么问题? A:区域信息即CultureInfo没有指定.如果不指定的话,它将采用默认的机器级的设置(见:控制面板->区域和语 ...
- MVC高级编程-目录
MVC高级编程 ================================================== 控制器 视图 模型 表单和HTML辅助方法 数据注解和验证 成员资格.授权和安全性 ...
- 如何在java中跳出当前多重嵌套循环?有几种方法?
如何在java中跳出当前多重嵌套循环?有几种方法? - 两种方法 - 1.在外层循环定义标记 ok: for(int i=0;i<100;i++){ ...
- Git_简介
Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制系统? 如果你用Microsoft Word写过长篇大论, ...
- Short Circuit Protection Circuit
http://www.daycounter.com/Circuits/Short-Circuit-Protection/Short-Circuit-Protection.phtml Short cir ...
- 使用 soapUI 测试 REST 服务
REST 服务介绍 REST(Representational State Transfer)是 Roy Fielding 博士在 2000 年提出的一种新的软件架构风格,它以资源(resource) ...