NineOldAndroids动画不工作的API→10 [英] NineOldAndroids animation not working on API>10

查看:654
本文介绍了NineOldAndroids动画不工作的API→10的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NineOldAndroid 库执行的动画。该动画做工精细的API< = 10。但对于API> 10的应用力关闭。这是我的code:

 进口静电com.nineoldandroids.view.ViewPropertyAnimator.animate;
进口android.content.Intent;
进口android.graphics.Canvas;
进口android.graphics.PixelFormat;
进口android.os.Build;
进口android.os.Bundle;
进口android.view.KeyEvent;
进口android.view.View;
进口android.view.ViewTreeObserver.OnGlobalLayoutListener;
进口android.view.WindowManager;
进口android.widget.ImageView;
进口android.widget.LinearLayout;
进口android.widget.RelativeLayout;
进口android.widget.TextView;

进口com.actionbarsherlock.app.ActionBar;
进口com.actionbarsherlock.view.MenuItem;
进口com.nineoldandroids.animation.Animator;
进口com.nineoldandroids.animation.Animator.AnimatorListener;
进口com.nineoldandroids.animation.ObjectAnimator;

公共类ActivityActualMain扩展SherlockActivity {
    的LinearLayout container1,container2的;
    RelativeLayout的viewTree;
    ImageView的图像,此搜索,图像2;
    TextView的电视,TV1,TV2,TV3,TV4;
    ObjectAnimator动画;



@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    最终诠释时间= 2000;
    的setContentView(R.layout.activity_actual_main);

    动作条酒吧= getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(真正的);
    bar.setBackgroundDrawable(getResources()。getDrawable(
            R.drawable.red_actionbar));
    viewTree =(RelativeLayout的)findViewById(R.id.viewTree);
    container1 =(的LinearLayout)findViewById(R.id.linearLayout1);
    container2的=(的LinearLayout)findViewById(R.id.linearLayout2);
    图像=(ImageView的)findViewById(R.id.imageView1);
    此搜索=(ImageView的)findViewById(R.id.imageView2);
    IMAGE2 =(ImageView的)findViewById(R.id.imageView3);
    电视=(TextView中)findViewById(R.id.text_tech_des);

    viewTree.getViewTreeObserver()。addOnGlobalLayoutListener(//检查是否布局已经被放置在活动
            新OnGlobalLayoutListener(){
                公共无效onGlobalLayout(){
                    如果(Build.VERSION.SDK_INT< 16){
                        。viewTree.getViewTreeObserver()removeGlobalOnLayoutListener(本);}
                    其他{
                        viewTree.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(本);
                    }

                    动画= ObjectAnimator.ofFloat(图像,Y,0F,
                            image.getTop());

                    anim.addListener(新AnimatorListener(){

                        @覆盖
                        公共无效onAnimationStart(动画为arg0){
                            // TODO自动生成方法存根
                            ObjectAnimator.ofFloat(电视,阿尔法,1,0,1)
                                    .setDuration(持续时间)。开始(); //行没有82
                            ObjectAnimator.ofFloat(container1,×,0F,
                                    container1.getLeft())setDuration(1000)。开始()。
                            ObjectAnimator.ofFloat(container2的,×,0F,
                                    container2.getLeft())setDuration(1000)。开始()。
                        }

                        @覆盖
                        公共无效onAnimationRepeat(动画为arg0){
                            // TODO自动生成方法存根

                        }

                        @覆盖
                        公共无效onAnimationEnd(动画为arg0){
                            // TODO自动生成方法存根
                        }

                        @覆盖
                        公共无效onAnimationCancel(动画为arg0){
                            // TODO自动生成方法存根

                        }
                    });
                    anim.setDuration(持续时间)。开始(); //行中没有106
                }
            });

}
}
 

这是我的堆栈跟踪:

  10-26 19:23:15.203:E / AndroidRuntime(21541):致命异常:主要
10-26 19:23:15.203:E / AndroidRuntime(21541):显示java.lang.NullPointerException
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.vishalaksh.technex.ActivityActualMain $ 2 $ 1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.vishalaksh.technex.ActivityActualMain $ 2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203:E / AndroidRuntime(21541):在android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203:E / AndroidRuntime(21541):在java.lang.reflect.Method.invokeNative(本机方法)
10-26 19:23:15.203:E / AndroidRuntime(21541):在java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203:E / AndroidRuntime(21541):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203:E / AndroidRuntime(21541):在dalvik.system.NativeStart.main(本机方法)
 

解决方案

我有一个问题与设备API&LT此库; 10,这给了相同的堆栈跟踪。由于我使用NineOldAndroids源$ C ​​$ C我决定调查在库中的问题。

一些测试,我注意到,出现此错误的原因的lib试图调用一些方法,不存在于视图之后(因为它的一个旧的API级别)。搜索多一点,我发现其中有一个所谓的包装静态方法AnimatorProxy类。此方法用于封装View对象在较旧的Andr​​oid的版本仿效一些动画方法,因为这样setScaleX / Y,setTransalationX / Y的presence。

要解决这个问题,我不得不打开ObjectAnimator类搜索该行的所有匹配(在我的库codeI发现4次):

  mTarget =目标;
 

在那个班我创建了下面的方法:

 私人无效setTarget(obj对象){
    如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.HONEYCOMB和放大器;&安培; OBJ的instanceof查看){
        mTarget = AnimatorProxy.wrap((查看)目标文件);
    } 其他 {
        mTarget = OBJ;
    }
}
 

和替换该行上面:

  setTarget(目标);
 

不知道它是否能够解决您的问题,因为你说,它发生在API 10+(我的对面),但它的一个好点开始。

I am using NineOldAndroid library to perform animations. The animations work fine for API<=10. But for API>10 the app force closes. This is my code:

import static com.nineoldandroids.view.ViewPropertyAnimator.animate;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.MenuItem;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ObjectAnimator;

public class ActivityActualMain extends SherlockActivity  {
    LinearLayout container1, container2;
    RelativeLayout viewTree;
    ImageView image, image1, image2;
    TextView tv, tv1, tv2, tv3, tv4;
    ObjectAnimator anim;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final int duration = 2000;
    setContentView(R.layout.activity_actual_main);

    ActionBar bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setBackgroundDrawable(getResources().getDrawable(
            R.drawable.red_actionbar));
    viewTree = (RelativeLayout) findViewById(R.id.viewTree);
    container1 = (LinearLayout) findViewById(R.id.linearLayout1);
    container2 = (LinearLayout) findViewById(R.id.linearLayout2);
    image = (ImageView) findViewById(R.id.imageView1);
    image1 = (ImageView) findViewById(R.id.imageView2);
    image2 = (ImageView) findViewById(R.id.imageView3);
    tv = (TextView) findViewById(R.id.text_tech_des);

    viewTree.getViewTreeObserver().addOnGlobalLayoutListener(//to check if the layout has been placed in activity
            new OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < 16) {
                        viewTree.getViewTreeObserver().removeGlobalOnLayoutListener(this);}
                    else{
                        viewTree.getViewTreeObserver()
                        .removeOnGlobalLayoutListener(this);
                    }

                    anim = ObjectAnimator.ofFloat(image, "y", 0f,
                            image.getTop());

                    anim.addListener(new AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator arg0) {
                            // TODO Auto-generated method stub
                            ObjectAnimator.ofFloat(tv, "alpha", 1, 0, 1)
                                    .setDuration(duration).start();//line no 82
                            ObjectAnimator.ofFloat(container1, "x", 0f,
                                    container1.getLeft()).setDuration(1000).start();
                            ObjectAnimator.ofFloat(container2, "x", 0f,
                                    container2.getLeft()).setDuration(1000).start();
                        }

                        @Override
                        public void onAnimationRepeat(Animator arg0) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onAnimationEnd(Animator arg0) {
                            // TODO Auto-generated method stub
                        }

                        @Override
                        public void onAnimationCancel(Animator arg0) {
                            // TODO Auto-generated method stub

                        }
                    });
                    anim.setDuration(duration).start();//line no 106
                }
            });

}
}

This is my stack trace:

    10-26 19:23:15.203: E/AndroidRuntime(21541): FATAL EXCEPTION: main
10-26 19:23:15.203: E/AndroidRuntime(21541): java.lang.NullPointerException
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:523)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:410)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:538)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:928)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.vishalaksh.technex.ActivityActualMain$2$1.onAnimationStart(ActivityActualMain.java:82)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:937)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ValueAnimator.start(ValueAnimator.java:951)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.nineoldandroids.animation.ObjectAnimator.start(ObjectAnimator.java:385)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.vishalaksh.technex.ActivityActualMain$2.onGlobalLayout(ActivityActualMain.java:106)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:808)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1768)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer.doFrame(Choreographer.java:532)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Handler.handleCallback(Handler.java:730)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.os.Looper.loop(Looper.java:137)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at java.lang.reflect.Method.invokeNative(Native Method)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at java.lang.reflect.Method.invoke(Method.java:525)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 19:23:15.203: E/AndroidRuntime(21541):    at dalvik.system.NativeStart.main(Native Method)

解决方案

I had an issue with this library on devices API < 10, which gave the same stacktrace. Given i was using the source code of NineOldAndroids i decided to investigate the problem inside the library.

After some tests i noticed that this error occurs because the lib tries to invoke some methods that doesn't exists on the view (since its an old API level). Searching a bit more i found the AnimatorProxy class which has a static method called "wrap". This method is used to encapsulate View objects in older Android versions emulating the presence of some animation methods as such setScaleX/Y, setTransalationX/Y.

To fix the problem i had to open the ObjectAnimator class and search for all occurrences of this line (In my library code i found 4 occurrences):

mTarget = target;

Inside that class i created the following method:

private void setTarget(Object obj) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB && obj instanceof View) {
        mTarget = AnimatorProxy.wrap((View) obj);
    } else {
        mTarget = obj;
    }
}

And replaced the line above with:

setTarget(target);

Don't know if it can fix your problem since you said that it occurs on API 10+ (the opposite of mine) but its a good point to start.

这篇关于NineOldAndroids动画不工作的API→10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆