在 api 8 中使用 Nineoldandroids 的 getViewTreeObserver 和 ObjectAnimator 不起作用。
它适用于更高的 API(例如 API 19)
这是我的代码。
final LayoutInflater inflater = (LayoutInflater)Home.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mMessage = (TextView) inflater.inflate(R.layout.refresh_message, null);
mMessage.setBackgroundColor(getResources().getColor(R.color.holo_green_light));
mMessage.setText(msg);
getWindowManager().addView(mMessage, getActionBarLayoutParams());
mMessage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
@Override
public void onGlobalLayout() {
// Get the height of the view
final int h = mMessage.getHeight();
//Remove Global Layout Listener
removeOnGlobalLayoutListener(mMessage,this);
// Slide down from the top
ObjectAnimator.ofFloat(mMessage, "translationY", -h, 0f).setDuration(250).start();
}
});
这是我的 LOGCAT::
05-09 19:28:47.942 507-507/com.bis.org.auInsight E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.view.ViewRoot
at com.nineoldandroids.view.animation.AnimatorProxy.invalidateAfterUpdate(AnimatorProxy.java:245)
at com.nineoldandroids.view.animation.AnimatorProxy.setTranslationY(AnimatorProxy.java:199)
at com.nineoldandroids.animation.PreHoneycombCompat$5.setValue(PreHoneycombCompat.java:57)
at com.nineoldandroids.animation.PreHoneycombCompat$5.setValue(PreHoneycombCompat.java:54)
at com.nineoldandroids.util.FloatProperty.set(FloatProperty.java:43)
at com.nineoldandroids.util.FloatProperty.set(FloatProperty.java:29)
at com.nineoldandroids.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:967)
at com.nineoldandroids.animation.ObjectAnimator.animateValue(ObjectAnimator.java:500)
at com.nineoldandroids.animation.ValueAnimator.animationFrame(ValueAnimator.java:1139)
at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:545)
at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:391)
at com.bis.org.auInsight.Home$3.onGlobalLayout(Home.java:187)
at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:549)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1094)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
笔记::
1) 我在 API8 和 API 19 之间进行了测试。
2) 我在两个 API 中都测试了 Nineoldandroids (ObjectAnimator) 并且它可以工作。
3) 我测试了从 getViewTreeObserver() 获得的 ObjectAnimator 和视图
- 结果 API 19 - 工作!,API 8 - 强制关闭;
4)我已经尝试过这个人的解决方案。
但是,我的强制关闭消失了,但仍然没有播放动画,并且动画出现了问题。