2017-02-23 57 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。 –