2016-04-27 46 views
12

的Android Studio版本2.1,gradle这个版本2.1.0,如果你发现任何误解:)编程着色,支持向量

我感到困惑的支持库23.3.0支持向量请指正。具体来说,我想要做的是以编程方式着色图像按钮,其src被定义为矢量绘制。从我可以告诉这是不可能的现在前棒棒糖。

我已看过一些相关的职位有关的变化: 23.2.0 announcement and changes

由于Android的支持库23.3.0中,支持向量绘图资源只能通过应用程序加载:srcCompat或setImageResource()。

上述是否意味着矢量个XML只能通过srcCompat或setImageResource()被用于预棒棒糖,因此不能动态着色

这是我的基本的图像按钮:

<ImageButton 
    android:id="@+id/nav_header_exit_community_button" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:background="@null"/> 

作品在棒棒糖及以上的只有:

Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp); 
    DrawableCompat.setTint(bg, headerTitleColor); 
    exitButton.setImageDrawable(bg); 

尝试这种预棒棒糖抛出: android.content.res.Resources$NotFoundException: File res/drawable/ic_exit_to_app_24dp.xml from drawable resource ID #0x7f0200bf

同样适用在棒棒糖和以上只有

Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp); 
    DrawableCompat.setTint(bg, headerTitleColor); 
    exitButton.setImageResource(R.drawable.ic_exit_to_app_24dp); 

这会在预棒棒糖上引发同样的错误。

但是,如果我删除vectorDrawables.useSupportLibrary = true如指出by Ian Lake here,与具有生成工具自动生成预棒棒糖设备png格式的意图,的PNG图像不会在着色前棒棒糖,所以我回方形之一。

我也尝试过通过srcCompat指定矢量并以编程方式检索它,但我不认为我已经能够实现这一点,即使它在后Lollipop上工作,如果使用src代替矢量。

所以对于23.3.0的情况似乎是:

  • 后棒棒糖:srcsrcCompat接受的载体,只有src可以从视图中为编程着色的可绘制检索 。 使用getDrawable可以引用代码中的矢量,并且它们可以是有色的 。

  • Pre-Lollipop:srcCompat只能接受矢量,不能从视图中以编程方式检索 。 setImageResource可以 接受载体,但仅限于vectorDrawables.useSupportLibrary = false,并且着色不起作用。除非vectorDrawables.useSupportLibrary = false和着色 不起作用,否则类似地引用代码中的矢量不是 。

使用上的所有版本工作的PNG:

Drawable bg = ContextCompat.getDrawable(a, R.drawable.ic_nav_exit_community); 
    DrawableCompat.setTint(bg, headerTitleColor); 
    exitButton.setImageDrawable(bg); 

附录:

这种技术也适用于后的棒棒糖,但前棒棒堂和其他人一样,我得到可绘制,但不可着色:

Drawable bg = VectorDrawableCompat.create(a.getResources(), R.drawable.ic_exit_to_app_24dp, null); 
    DrawableCompat.setTint(bg, headerTitleColor); 
    exitButton.setImageDrawable(bg); 

同类解决方案:

由于John's答案迄今唯一很简单的方法,我可以拿出来着色,支持向量是设置彩色滤光片就可以了 - 这意味着DrawableCompat.setTint()功能似乎是如果所讨论的drawable是一个支持向量,那么对我而言不起作用。我不确定这是否是一个合法的错误,预期的行为,或者如果我只是做错了什么!

这是我跟去暂时解决方案:

Drawable bg; 
    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
     bg = VectorDrawableCompat.create(a.getResources(), R.drawable.ic_exit_to_app_24dp, null); 
     exitButton.setColorFilter(headerTitleColor, PorterDuff.Mode.MULTIPLY); 
    } 
    else { 
     bg = ContextCompat.getDrawable(a, R.drawable.ic_exit_to_app_24dp); 
     DrawableCompat.setTint(bg, headerTitleColor); 
    } 
    exitButton.setImageDrawable(bg); 
+0

请问,如果你使用Vector COMPAT库中创建您的drawble工作?即VectorDrawableCompat.create(getResources(),R.drawable.ic_action_add,null) – Kuffs

+0

我已经更新了我的答案,看起来像是另一种技术(!)来做同样的事情,但设色它不起作用在pre-L –

+0

这不是另一个技术,这是如何创建'VectorDrawableCompat',并且如果您阅读'DrawableCompat'文档,您会看到[this](http://pastebin.com/CzEbnP9k)正常工作 – pskink

回答

26

首先你应该使用VectorDrawableCompat#create,一旦你有你的Drawable你必须致电DrawableCompat#wrap

可能包装成可绘制的,因此可以通过此类中的着色方法在不同的API水平上对 进行着色。

所以你的代码应该是这样的:

ImageView iv = .... 
Drawable d = VectorDrawableCompat.create(getResources(), R.drawable.ic_exit_to_app_24dp, null); 
d = DrawableCompat.wrap(d); 
DrawableCompat.setTint(d, headerTitleColor); 
iv.setImageDrawable(d); 
5

可以使用的ImageView的setColorFilter方法:

imageView.setColorFilter(headerTitleColor, android.graphics.PorterDuff.Mode.MULTIPLY);

+0

目前这似乎是最好的解决方案,虽然我不高兴! –