【问题标题】:getDrawable with AnimatedVectorDrawable crashes in android version 4android 版本 4 中带有 AnimatedVectorDrawable 的 getDrawable 崩溃
【发布时间】:2016-10-18 08:31:10
【问题描述】:

我尝试使用 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: 文件 res/drawable/ic_menu_animated.xml 来自可绘制资源 ID

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

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

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 但是xml文件和animatedvectordrawable配合得很好
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多