2017-02-23 25 views
2

我有一个方法将VectorDrawable设置为TextView和Button上的DrawableLeft,DrawableRight等,但我想使它对普通的Drawable也起作用。那么,有没有办法检查DrawableRes的类型,还是应该使用try/catch? 该方法如下所示:检查DrawableRes是否为VectorDrawable

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) { 
    final Resources resources = view.getResources(); 
    Drawable vectorDrawableLeft = left != 0 ? VectorDrawableCompat.create(resources, left, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableTop = top != 0 ? VectorDrawableCompat.create(resources, top, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableRight = right != 0 ? VectorDrawableCompat.create(resources, right, view.getContext().getTheme()) : null; 
    Drawable vectorDrawableBottom = bottom != 0 ? VectorDrawableCompat.create(resources, bottom, view.getContext().getTheme()) : null; 

    if (view instanceof Button) { 
     ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } 
    else if (view instanceof TextView) { 
     ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else { 
     Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName()); 
    } 
} 
+2

所以你想'android.support.v7.content.res.AppCompatResources '? – pskink

+0

它正在工作。谢谢!我刚刚用'AppCompatResources.getDrawable(view.getContext(),left)'替换了'VectorDrawableCompat.create(resources,left,view.getContext()。getTheme())''。 –

+0

还有'android.support.v4.content.res.ResourcesCompat '但我不知道它是否可以正常工作,你可以查看它... – pskink

回答

1

总结评论回答。据我发现,没有简单的方法可以知道DrawableRes是否指向VectorDrawable。如果有人知道如何,请写信。但是要解决我的问题,建议使用android.support.v7.content.res.AppCompatResources就足够了@pskink。所以,现在的方法是这样的:

public static void setViewDrawables(@NonNull View view, @DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) { 
    Drawable vectorDrawableLeft = left != 0 ? AppCompatResources.getDrawable(view.getContext(), left) : null; 
    Drawable vectorDrawableTop = top != 0 ? AppCompatResources.getDrawable(view.getContext(), top) : null; 
    Drawable vectorDrawableRight = right != 0 ? AppCompatResources.getDrawable(view.getContext(), right) : null; 
    Drawable vectorDrawableBottom = bottom != 0 ? AppCompatResources.getDrawable(view.getContext(), bottom) : null; 

    if (view instanceof Button) { 
     ((Button) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else if (view instanceof TextView) { 
     ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(vectorDrawableLeft, vectorDrawableTop, vectorDrawableRight, vectorDrawableBottom); 
    } else { 
     Log.e("ERROR: ViewUtils", "Can't do setCompoundDrawablesWithIntrinsicBounds on " + view.getClass().getName()); 
    } 
} 

更新:

android.support.v7.widget.AppCompatDrawableManager有一种方法boolean isVectorDrawable(@NonNull Drawable d)这是检查是否绘制对象是VectorDrawable,但它采取绘制对象作为参数,而不是DrawableRes。它看起来像这样:

private static boolean isVectorDrawable(@NonNull Drawable d) { 
    return d instanceof VectorDrawableCompat 
      || PLATFORM_VD_CLAZZ.equals(d.getClass().getName()); 
} 
+1

如果你想知道他们是如何做到的,与你的调试器一起放入'AppCompatResources.getDrawable'中,并在'VectorDrawableCompat'必须被创建时检查他们何时/如何调用'VectorDrawableCompat.create' – pskink

+0

'isVectorDrawable'方法后面有几行'private static class VdcInflateDelegate implements InflateDelegate'它膨胀了'VectorDrawableCompat'对象 – pskink

+0

@pskink是的,但正如你可以看到它是一个try/catch方法,并且我问了是否有其他更简洁的方法来实现它。在这种方法中,他们试图创建VectorDrawable,如果它不工作,那么它不是VectorDrawable。 –