Android Studio now supports vector assets on 21+ and will generate pngs for lower versions at compile time. I have a vector asset (from the Material Icons) that I want to change the fill color. This works on 21+, but the generated pngs do not change color. Is there a way to do this?
Android Studio现在支持21+上的向量资产,并将在编译时为较低的版本生成png。我有一个矢量资产(来自材质图标),我想要改变填充颜色。这适用于21+,但是生成的pngs不会改变颜色。有办法吗?
<vector android:height="48dp" android:viewportHeight="24.0"
android:viewportWidth="24.0" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@color/primary" android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z"/>
10 个解决方案
#1
213
Don't edit the vector assets directly. If you using a vector drawable in an ImageButton, just choose your color in android:tint
.
不要直接编辑矢量资产。如果在ImageButton中使用矢量绘制,只需在android中选择颜色:tint。
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
android:src="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />
#2
80
You can do it.
你能做到。
BUT you cannot use @color references for colors (..lame), otherwise it will work only for L+
但是不能对颜色(.. .lame)使用@color引用,否则只能用于L+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFAABB"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zm-6,0C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
#3
51
As said in other answers, don't edit the vector drawable directly, instead you can tint in java code, like that:
如其他答案所述,不要直接编辑矢量绘图,而是可以在java代码中着色,如下所示:
mWrappedDrawable = mDrawable.mutate();
mWrappedDrawable = DrawableCompat.wrap(mWrappedDrawable);
DrawableCompat.setTint(mWrappedDrawable, mColor);
DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN);
And for the sake of simplicity, I have created a helper class:
为了简单起见,我创建了一个助手类:
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
/**
* {@link Drawable} helper class.
*
* @author Filipe Bezerra
* @version 18/01/2016
* @since 18/01/2016
*/
public class DrawableHelper {
@NonNull Context mContext;
@ColorRes private int mColor;
private Drawable mDrawable;
private Drawable mWrappedDrawable;
public DrawableHelper(@NonNull Context context) {
mContext = context;
}
public static DrawableHelper withContext(@NonNull Context context) {
return new DrawableHelper(context);
}
public DrawableHelper withDrawable(@DrawableRes int drawableRes) {
mDrawable = ContextCompat.getDrawable(mContext, drawableRes);
return this;
}
public DrawableHelper withDrawable(@NonNull Drawable drawable) {
mDrawable = drawable;
return this;
}
public DrawableHelper withColor(@ColorRes int colorRes) {
mColor = ContextCompat.getColor(mContext, colorRes);
return this;
}
public DrawableHelper tint() {
if (mDrawable == null) {
throw new NullPointerException("É preciso informar o recurso drawable pelo método withDrawable()");
}
if (mColor == 0) {
throw new IllegalStateException("É necessário informar a cor a ser definida pelo método withColor()");
}
mWrappedDrawable = mDrawable.mutate();
mWrappedDrawable = DrawableCompat.wrap(mWrappedDrawable);
DrawableCompat.setTint(mWrappedDrawable, mColor);
DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN);
return this;
}
@SuppressWarnings("deprecation")
public void applyToBackground(@NonNull View view) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(mWrappedDrawable);
} else {
view.setBackgroundDrawable(mWrappedDrawable);
}
}
public void applyTo(@NonNull ImageView imageView) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
imageView.setImageDrawable(mWrappedDrawable);
}
public void applyTo(@NonNull MenuItem menuItem) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
menuItem.setIcon(mWrappedDrawable);
}
public Drawable get() {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
return mWrappedDrawable;
}
}
To use just do the following:
要使用以下方法:
DrawableHelper
.withContext(this)
.withColor(R.color.white)
.withDrawable(R.drawable.ic_search_24dp)
.tint()
.applyTo(mSearchItem);
Or:
或者:
final Drawable drawable = DrawableHelper
.withContext(this)
.withColor(R.color.white)
.withDrawable(R.drawable.ic_search_24dp)
.tint()
.get();
actionBar.setHomeAsUpIndicator(drawable);
#4
23
To change vector image color you can directly use android:tint="@color/colorAccent"
要改变矢量图像的颜色,你可以直接使用android:tint="@color/colorAccent"
<ImageView
android:id="@+id/ivVectorImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_account_circle_black_24dp"
android:tint="@color/colorAccent" />
To change color programatically
语法上改变颜色
ImageView ivVectorImage = (ImageView) findViewById(R.id.ivVectorImage);
ivVectorImage.setColorFilter(getResources().getColor(R.color.colorPrimary));
#5
13
Currently the working soloution is android:fillColor="#FFFFFF"
目前的工作解决方案是android:fillColor="# ffff "
Nothing worked for me except hard coding in the vector
除了在向量中进行硬编码外,我什么都做不到
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:fillColor="#FFFFFF"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zm-6,0C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
However, fillcolor and tint might work soon. Please see this discussion for more information:
不过,fillcolor和tint可能很快就会奏效。有关更多信息,请参阅本讨论:
https://code.google.com/p/android/issues/detail?id=186431
https://code.google.com/p/android/issues/detail?id=186431
Also the colors mighr stick in the cache so deleting app for all users might help.
另外,颜色可能会粘在缓存中,因此为所有用户删除应用程序可能会有所帮助。
#6
6
Android studio now supports vectors pre-lollipop. No PNG conversion. You can still change your fill color and it will work.
Android studio现在支持向量机。没有PNG转换。你仍然可以改变你的填充颜色,它将会工作。
In you ImageView, use
在你ImageView,使用
app:srcCompat="@drawable/ic_more_vert_24dp"
In your gradle file,
在你gradle文件,
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
compile 'com.android.support:design:23.4.0'
#7
2
if you look to support old version pre lolipop
如果您希望支持旧版本的pre lolipop
use the same xml code with some changes
使用相同的xml代码进行一些更改
instead of normal ImageView --> AppCompatImageView
而不是普通的ImageView——> AppCompatImageView。
instead of android:src --> app:srcCompat
而不是android:src——> app:srcCompat
here is example
下面是例子
<android.support.v7.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
app:srcCompat="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />
dont forget update your gradle as @ Sayooj Valsan mention
别忘了更新你的gradle,就像@ Sayooj Valsan提到的那样。
// Gradle Plugin 2.0+ android { defaultConfig { vectorDrawables.useSupportLibrary = true } } compile 'com.android.support:design:23.4.0'
Notice To any one use vector dont ever ever never give your vector reference to color like this one android:fillColor="@color/primary"
give its hex value .
注意,对于任何使用矢量的人,永远不要给你的矢量引用像这样的颜色:fillColor="@color/primary"给出十六进制值。
#8
2
Add this library to the Gradle to enable color vector drawable in old android Devices.
将这个库添加到Gradle中,可以在旧的android设备中绘制颜色向量。
compile 'com.android.support:palette-v7:26.0.0-alpha1'
and re sync gradle. I think it will solve the problem.
和再保险gradle同步。我认为这将解决问题。
#9
2
Update: AppCompat
support
Other answers suspecting if android:tint
will work on only 21+ devices only, AppCompat(v23.2.0 and above) now provides a backward compatible handling of tint attribute.
如果android:tint只能在21个以上的设备上运行,那么其他的答案可能会怀疑,AppCompat(v23.2.0及以上版本)现在提供了对tint属性的向后兼容处理。
So, the course of action would be to use AppCompatImageView
and app:srcCompat
(in AppCompat namespace) instead of android:src
(Android namespace).
因此,我们将使用AppCompatImageView和app:srcCompat(在AppCompat名称空间中)而不是android:src(android名称空间)。
Here is an example:
这是一个例子:
<android.support.v7.widget.AppCompatImageView
android:id="@+id/credits_material_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:scaleType="fitCenter"
android:tint="#ffd2ee"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@drawable/ic_dollar_coin_stack" />
And don't forget to enable vector drawable support in gradle:
并且别忘了在gradle中启用矢量可拉支持:
vectorDrawables.useSupportLibrary = true
#10
1
If the vectors are not showing individually set colors using fillColor then they may be being set to a default widget parameter.
如果向量没有使用fillColor显示单独设置的颜色,那么它们可能被设置为一个默认的小部件参数。
Try adding app:itemIconTint="@color/lime"
to activity_main.xml to set a default color type for the widget icons.
尝试添加应用:itemIconTint="@color/lime"到activity_main。为小部件图标设置默认的颜色类型。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/lime"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
VectorDrawable @ developers.android
VectorDrawable @ developers.android
#1
213
Don't edit the vector assets directly. If you using a vector drawable in an ImageButton, just choose your color in android:tint
.
不要直接编辑矢量资产。如果在ImageButton中使用矢量绘制,只需在android中选择颜色:tint。
<ImageButton
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
android:src="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />
#2
80
You can do it.
你能做到。
BUT you cannot use @color references for colors (..lame), otherwise it will work only for L+
但是不能对颜色(.. .lame)使用@color引用,否则只能用于L+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFAABB"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zm-6,0C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
#3
51
As said in other answers, don't edit the vector drawable directly, instead you can tint in java code, like that:
如其他答案所述,不要直接编辑矢量绘图,而是可以在java代码中着色,如下所示:
mWrappedDrawable = mDrawable.mutate();
mWrappedDrawable = DrawableCompat.wrap(mWrappedDrawable);
DrawableCompat.setTint(mWrappedDrawable, mColor);
DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN);
And for the sake of simplicity, I have created a helper class:
为了简单起见,我创建了一个助手类:
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
/**
* {@link Drawable} helper class.
*
* @author Filipe Bezerra
* @version 18/01/2016
* @since 18/01/2016
*/
public class DrawableHelper {
@NonNull Context mContext;
@ColorRes private int mColor;
private Drawable mDrawable;
private Drawable mWrappedDrawable;
public DrawableHelper(@NonNull Context context) {
mContext = context;
}
public static DrawableHelper withContext(@NonNull Context context) {
return new DrawableHelper(context);
}
public DrawableHelper withDrawable(@DrawableRes int drawableRes) {
mDrawable = ContextCompat.getDrawable(mContext, drawableRes);
return this;
}
public DrawableHelper withDrawable(@NonNull Drawable drawable) {
mDrawable = drawable;
return this;
}
public DrawableHelper withColor(@ColorRes int colorRes) {
mColor = ContextCompat.getColor(mContext, colorRes);
return this;
}
public DrawableHelper tint() {
if (mDrawable == null) {
throw new NullPointerException("É preciso informar o recurso drawable pelo método withDrawable()");
}
if (mColor == 0) {
throw new IllegalStateException("É necessário informar a cor a ser definida pelo método withColor()");
}
mWrappedDrawable = mDrawable.mutate();
mWrappedDrawable = DrawableCompat.wrap(mWrappedDrawable);
DrawableCompat.setTint(mWrappedDrawable, mColor);
DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN);
return this;
}
@SuppressWarnings("deprecation")
public void applyToBackground(@NonNull View view) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.setBackground(mWrappedDrawable);
} else {
view.setBackgroundDrawable(mWrappedDrawable);
}
}
public void applyTo(@NonNull ImageView imageView) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
imageView.setImageDrawable(mWrappedDrawable);
}
public void applyTo(@NonNull MenuItem menuItem) {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
menuItem.setIcon(mWrappedDrawable);
}
public Drawable get() {
if (mWrappedDrawable == null) {
throw new NullPointerException("É preciso chamar o método tint()");
}
return mWrappedDrawable;
}
}
To use just do the following:
要使用以下方法:
DrawableHelper
.withContext(this)
.withColor(R.color.white)
.withDrawable(R.drawable.ic_search_24dp)
.tint()
.applyTo(mSearchItem);
Or:
或者:
final Drawable drawable = DrawableHelper
.withContext(this)
.withColor(R.color.white)
.withDrawable(R.drawable.ic_search_24dp)
.tint()
.get();
actionBar.setHomeAsUpIndicator(drawable);
#4
23
To change vector image color you can directly use android:tint="@color/colorAccent"
要改变矢量图像的颜色,你可以直接使用android:tint="@color/colorAccent"
<ImageView
android:id="@+id/ivVectorImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_account_circle_black_24dp"
android:tint="@color/colorAccent" />
To change color programatically
语法上改变颜色
ImageView ivVectorImage = (ImageView) findViewById(R.id.ivVectorImage);
ivVectorImage.setColorFilter(getResources().getColor(R.color.colorPrimary));
#5
13
Currently the working soloution is android:fillColor="#FFFFFF"
目前的工作解决方案是android:fillColor="# ffff "
Nothing worked for me except hard coding in the vector
除了在向量中进行硬编码外,我什么都做不到
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:fillColor="#FFFFFF"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zm-6,0C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
However, fillcolor and tint might work soon. Please see this discussion for more information:
不过,fillcolor和tint可能很快就会奏效。有关更多信息,请参阅本讨论:
https://code.google.com/p/android/issues/detail?id=186431
https://code.google.com/p/android/issues/detail?id=186431
Also the colors mighr stick in the cache so deleting app for all users might help.
另外,颜色可能会粘在缓存中,因此为所有用户删除应用程序可能会有所帮助。
#6
6
Android studio now supports vectors pre-lollipop. No PNG conversion. You can still change your fill color and it will work.
Android studio现在支持向量机。没有PNG转换。你仍然可以改变你的填充颜色,它将会工作。
In you ImageView, use
在你ImageView,使用
app:srcCompat="@drawable/ic_more_vert_24dp"
In your gradle file,
在你gradle文件,
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
compile 'com.android.support:design:23.4.0'
#7
2
if you look to support old version pre lolipop
如果您希望支持旧版本的pre lolipop
use the same xml code with some changes
使用相同的xml代码进行一些更改
instead of normal ImageView --> AppCompatImageView
而不是普通的ImageView——> AppCompatImageView。
instead of android:src --> app:srcCompat
而不是android:src——> app:srcCompat
here is example
下面是例子
<android.support.v7.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/button"
app:srcCompat="@drawable/ic_more_vert_24dp"
android:tint="@color/primary" />
dont forget update your gradle as @ Sayooj Valsan mention
别忘了更新你的gradle,就像@ Sayooj Valsan提到的那样。
// Gradle Plugin 2.0+ android { defaultConfig { vectorDrawables.useSupportLibrary = true } } compile 'com.android.support:design:23.4.0'
Notice To any one use vector dont ever ever never give your vector reference to color like this one android:fillColor="@color/primary"
give its hex value .
注意,对于任何使用矢量的人,永远不要给你的矢量引用像这样的颜色:fillColor="@color/primary"给出十六进制值。
#8
2
Add this library to the Gradle to enable color vector drawable in old android Devices.
将这个库添加到Gradle中,可以在旧的android设备中绘制颜色向量。
compile 'com.android.support:palette-v7:26.0.0-alpha1'
and re sync gradle. I think it will solve the problem.
和再保险gradle同步。我认为这将解决问题。
#9
2
Update: AppCompat
support
Other answers suspecting if android:tint
will work on only 21+ devices only, AppCompat(v23.2.0 and above) now provides a backward compatible handling of tint attribute.
如果android:tint只能在21个以上的设备上运行,那么其他的答案可能会怀疑,AppCompat(v23.2.0及以上版本)现在提供了对tint属性的向后兼容处理。
So, the course of action would be to use AppCompatImageView
and app:srcCompat
(in AppCompat namespace) instead of android:src
(Android namespace).
因此,我们将使用AppCompatImageView和app:srcCompat(在AppCompat名称空间中)而不是android:src(android名称空间)。
Here is an example:
这是一个例子:
<android.support.v7.widget.AppCompatImageView
android:id="@+id/credits_material_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:scaleType="fitCenter"
android:tint="#ffd2ee"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@drawable/ic_dollar_coin_stack" />
And don't forget to enable vector drawable support in gradle:
并且别忘了在gradle中启用矢量可拉支持:
vectorDrawables.useSupportLibrary = true
#10
1
If the vectors are not showing individually set colors using fillColor then they may be being set to a default widget parameter.
如果向量没有使用fillColor显示单独设置的颜色,那么它们可能被设置为一个默认的小部件参数。
Try adding app:itemIconTint="@color/lime"
to activity_main.xml to set a default color type for the widget icons.
尝试添加应用:itemIconTint="@color/lime"到activity_main。为小部件图标设置默认的颜色类型。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/lime"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
VectorDrawable @ developers.android
VectorDrawable @ developers.android