在Android Studio中更改vector素材上的填充颜色

Android Studio现在支持21+以上的vector素材,并且会在编译时生成较低版本的png。 我有一个vector资源(从材料图标),我想改变填充颜色。 这适用于21+,但生成的PNG不会改变颜色。 有没有办法做到这一点?

<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"/> 

不要直接编辑vector资产。 如果您在ImageButton中使用vector绘图,只需在android:tintselect您的颜色即可。

 <ImageButton android:layout_width="48dp" android:layout_height="48dp" android:id="@+id/button" android:src="@drawable/ic_more_vert_24dp" android:tint="@color/primary" /> 

你能行的。

但是你不能使用颜色(..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"/> 

正如在其他答案中所说的,不要直接编辑vector绘制,而是可以在java代码中着色,如下所示:

  mWrappedDrawable = mDrawable.mutate(); mWrappedDrawable = DrawableCompat.wrap(mWrappedDrawable); DrawableCompat.setTint(mWrappedDrawable, mColor); DrawableCompat.setTintMode(mWrappedDrawable, PorterDuff.Mode.SRC_IN); 

为了简单起见,我创build了一个帮助类:

 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; } } 

要使用只需执行以下操作:

  DrawableHelper .withContext(this) .withColor(R.color.white) .withDrawable(R.drawable.ic_search_24dp) .tint() .applyTo(mSearchItem); 

要么:

  final Drawable drawable = DrawableHelper .withContext(this) .withColor(R.color.white) .withDrawable(R.drawable.ic_search_24dp) .tint() .get(); actionBar.setHomeAsUpIndicator(drawable); 

要更改vector图像颜色,您可以直接使用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" /> 

编程方式更改颜色

 ImageView ivVectorImage = (ImageView) findViewById(R.id.ivVectorImage); ivVectorImage.setColorFilter(getResources().getColor(R.color.colorPrimary)); 

目前工作的解决scheme是android:fillColor =“#FFFFFF”

除了向量中的硬编码之外,没有任何东西可以工作

 <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"/> 

但是,fillcolor和tint可能会很快起作用。 请参阅此讨论以获取更多信息:

https://code.google.com/p/android/issues/detail?id=186431

此外颜色mighr坚持在caching中,所以删除所有用户的应用程序可能会有所帮助。

Android工作室现在支持vector预棒棒糖。 没有PNG转换。 你仍然可以改变你的填充颜色,它会起作用。

在你ImageView中使用

  app:srcCompat="@drawable/ic_more_vert_24dp" 

在你的gradle文件中,

  // Gradle Plugin 2.0+ android { defaultConfig { vectorDrawables.useSupportLibrary = true } } compile 'com.android.support:design:23.4.0' 

如果你看起来支持旧版本的lolipop

使用相同的XML代码进行一些更改

而不是正常的ImageView --> AppCompatImageView

而不是android:src --> app:srcCompat

这里是例子

 <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" /> 

不要忘了更新你的gradle作为@ Sayooj Valsan提及

 // Gradle Plugin 2.0+ android { defaultConfig { vectorDrawables.useSupportLibrary = true } } compile 'com.android.support:design:23.4.0' 

注意对于任何一个使用vector永远不会永远不会给你的vector引用颜色像这样一个android:fillColor="@color/primary"给它的hex值。

将这个库添加到Gradle中,以启用旧的Android设备中可绘制的颜色向量。

 compile 'com.android.support:palette-v7:26.0.0-alpha1' 

并重新同步gradle。 我认为这将解决问题。

更新: AppCompat支持

其他答案怀疑如果android:tint只能在21+设备上工作, AppCompat( v23.2.0及以上现在提供了一个向后兼容的tint属性处理。

所以,行动的过程将是使用AppCompatImageViewapp:srcCompat (在AppCompat命名空间中)而不是android:src (Android命名空间)。

这里是一个例子:

 <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" /> 

并且不要忘记在gradle中启用vector绘图支持:

 vectorDrawables.useSupportLibrary = true 

如果向量未使用fillColor单独显示设置的颜色,则可能将它们设置为默认的小部件参数。

尝试将app:itemIconTint="@color/lime"到activity_main.xml中,为小部件图标设置默认的颜色types。

 <?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