Vla*_*vić 5 android android-drawable android-vectordrawable
我有一个方法可以在 TextView 和 Button 上将 VectorDrawable 设置为 DrawableLeft、DrawableRight 等,但我想让它也适用于普通的 Drawable。那么,有没有办法检查 DrawableRes 的类型,还是应该使用 try/catch?该方法如下所示:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
总结对一个答案的评论。据我发现,没有简单的方法可以知道 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());
}
}
Run Code Online (Sandbox Code Playgroud)
更新:
在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());
}
Run Code Online (Sandbox Code Playgroud)