带有AnimatedVectorDrawable的getDrawable在Android版本4中崩溃

时间:2016-10-18 08:31:10

标签: android

我尝试使用ContextCompat按如下方式访问AnimatedVectorDrawable。我使用以下代码来执行此操作。它适用于Android 5及更高版本,但不适用于Android 4版。

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void startOpenAnimations() {

        // Icon
        AnimatedVectorDrawable menuIcon = (AnimatedVectorDrawable) ContextCompat.getDrawable(getContext(),
                R.drawable.ic_menu_animated);
        mFabView.setImageDrawable(menuIcon);
        menuIcon.start();

        // Reveal
        int centerX = mFabRect.centerX();
        int centerY = mFabRect.centerY();
        float startRadius = getMinRadius();
        float endRadius = getMaxRadius();
        Animator reveal = ViewAnimationUtils
                .createCircularReveal(mNavigationView,
                        centerX, centerY, startRadius, endRadius);

        // Fade in
        mNavigationMenuView.setAlpha(0);
        Animator fade = ObjectAnimator.ofFloat(mNavigationMenuView, View.ALPHA, 0, 1);

        // Animations
        AnimatorSet set = new AnimatorSet();
        set.playSequentially(reveal, fade);
        set.start();
    }
  

android.content.res.Resources $ NotFoundException:File   来自可绘制资源ID的res / drawable / ic_menu_animated.xml

     

0x7f020064

                                                                    at android.content.res.Resources.loadDrawable(Resources.java:3451)
                                                                    at android.content.res.Resources.getDrawable(Resources.java:1894)
                                                                    at
     

android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:354)

为什么在Lollipop设备中无法使用ContextCompat进行getDrawable

我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

试试这个:AnimatedVectorDrawableCompat.create(this, R.drawable.animated_vector_name)

参考: https://stackoverflow.com/a/35699072/7001152