Android:如何以编程方式设置矢量可绘制的笔触颜色 [英] Android: How to set stroke color for vector drawable programmatically

查看:138
本文介绍了Android:如何以编程方式设置矢量可绘制的笔触颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中使用VectorDrawable遇到麻烦了. 我有一个矢量可绘制文件(.xml),我想在位图上绘制它.我设法加载此文件并将其绘制在位图上.我可以更改其填充颜色,但是问题是我无法更改其笔触和颜色.

I am getting into a trouble with VectorDrawable in Android. I have a vector drawable file (.xml) and I want to draw it on bitmap. I managed to load this file and draw it on bitmap. I can change its fill color but the problem is that I cannot change its stroke and color.

任何帮助将不胜感激!

谢谢!

这是可绘制的文件:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="312dp"
    android:viewportWidth="312.7"
    android:viewportHeight="312.699">
<path
    android:pathData="M306.35,266.34c0,22.09 -17.91,40.01 -40,40.01L46.35,306.35c-22.09,0 -40,-17.92 -40,-40.01v-219.99c0,-22.11 17.92,-40 40,-40h220c22.09,0 40,17.9 40,40L306.35,266.34z"
    android:strokeWidth="5"
    android:fillColor="@color/transparent"
    android:strokeColor="#231F20"/></vector>

这是我用蓝色填充和填充形状的方式:

Here is the way I load and fill the shape with blue color:

Drawable drawable = getResources().getDrawable(R.drawable.graph_rounded_rectangle);
        drawable.setBounds(0, 0, width, height);
        drawable.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.blue), PorterDuff.Mode.MULTIPLY));
        drawable.draw(canvas);

推荐答案

As said by Chris Banes in his blog, you can tint your drawable using support library with the following code:

Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ic_asset);

// Wrap the drawable so that future tinting calls work
// on pre-v21 devices. Always use the returned drawable.
drawable = DrawableCompat.wrap(drawable);

// We can now set a tint
DrawableCompat.setTint(drawable, Color.RED);
// ...or a tint list
DrawableCompat.setTintList(drawable, myColorStateList);
// ...and a different tint mode
DrawableCompat.setTintMode(drawable, PorterDuff.Mode.SRC_OVER);

这篇关于Android:如何以编程方式设置矢量可绘制的笔触颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆