无法使用AnimatedVectorDrawable为FAB绘制动画

时间:2017-04-25 09:22:39

标签: android animation

我正在尝试为FAB绘制动画,但它会抛出此错误。在drawable上调用 .start()的行上会出现错误。

--------- beginning of crash
04-25 14:40:09.169 25611-25611/com.test.testingfabanimation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                       Process: com.test.testingfabanimation, PID: 25611
                                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
                                                                                           at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT.createRTAnimator(AnimatedVectorDrawable.java:1262)
                                                                                           at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT.parseAnimatorSet(AnimatedVectorDrawable.java:1220)
                                                                                           at android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorRT.init(AnimatedVectorDrawable.java:1187)
                                                                                           at android.graphics.drawable.AnimatedVectorDrawable.ensureAnimatorSet(AnimatedVectorDrawable.java:851)
                                                                                           at android.graphics.drawable.AnimatedVectorDrawable.start(AnimatedVectorDrawable.java:834)
                                                                                           at com.test.testingfabanimation.MainActivity$1.onPageSelected(MainActivity.java:41)
                                                                                           at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1967)
                                                                                           at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685)
                                                                                           at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
                                                                                           at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2284)
                                                                                           at android.view.View.dispatchTouchEvent(View.java:10024)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2626)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2632)
                                                                                           at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
                                                                                           at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
                                                                                           at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1809)
                                                                                           at android.app.Activity.dispatchTouchEvent(Activity.java:3061)
                                                                                           at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                                                                                           at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
                                                                                           at android.view.View.dispatchPointerEvent(View.java:10244)
                                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4438)
                                                                                           at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4306)
                                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                                           at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3999)
                                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                                           at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4056)
                                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                           at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3906)
                                                                                           at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3872)
                                                                                           at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3880)
                                                                                           at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3853)
                                                                                           at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6246)
                                                                                           at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6220)
                                                                                           at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6181)
04-25 14:40:09.171 25611-25611/com.test.testingfabanimation E/AndroidRuntime:     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6349)
                                                                                           at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                                                                                           at android.os.MessageQueue.nativePollOnce(Native Method)
                                                                                           at android.os.MessageQueue.next(MessageQueue.java:323)
                                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6169)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

这是 MainActivity 中的代码,我试图为FAB设置动画。

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        fab.setImageResource(R.drawable.animate_forward);
                        break;
                    case 1:
                        fab.setImageResource(R.drawable.animate_backward);
                        break;
                }
                Drawable drawable = fab.getDrawable();
                if (drawable instanceof AnimatedVectorDrawable)
                    ((AnimatedVectorDrawable) drawable).start();
            }
        });

我使用Roman Nurik的Android Icon Animator为动画创建了自定义绘图。

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt">
    <aapt:attr name="android:drawable">
        <vector
            android:width="24dp"
            android:height="24dp"
            android:viewportWidth="24"
            android:viewportHeight="24"
            android:alpha="1">
            <path
                android:name="foo"
                android:pathData="@string/forward"
                android:strokeColor="#000000"
                android:strokeWidth="2"
                android:strokeLineCap="round"/>
        </vector>
    </aapt:attr>
    <target android:name="path_morph">
        <aapt:attr name="android:animation">
            <objectAnimator
                android:name="foo"
                android:propertyName="pathData"
                android:duration="300"
                android:valueFrom="@string/forward"
                android:valueTo="@string/backward"
                android:valueType="pathType"
                android:interpolator="@android:anim/accelerate_decelerate_interpolator"/>
        </aapt:attr>
    </target>
</animated-vector>

animate_forward和animate_backword xmls类似。只有路径中的字符串值互换。

这是我第一次使用AnimatedVectorDrawables。有什么我想念的吗?所有教程都在网络上显示相同的代码。

1 个答案:

答案 0 :(得分:3)

目标名称必须是要设置动画的对象的名称。将目标名称设置为“foo”。您也不需要为ObjectAnimator本身设置任何名称。