gpt4 book ai didi

android - 检查 DrawableRes 是否为 VectorDrawable

转载 作者:太空宇宙 更新时间:2023-11-03 13:45:52 27 4
gpt4 key购买 nike

我有一个方法可以在 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());
}
}

最佳答案

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

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) 检查 Drawable 是否是 VectorDrawable,但是它需要Drawable 作为参数,而不是 DrawableRes。它看起来像这样:

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

关于android - 检查 DrawableRes 是否为 VectorDrawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42412563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com