我可以通过编程更改矢量可绘制对象的颜色,但我想将描边应用于矢量可绘制对象。我需要一种在运行时更改矢量可绘制对象描边的方法:
以前我使用了这种方法,但在我的情况下失败了。
我将矢量可绘制对象转换为位图,然后使用此函数应用边框,但它会全部填充为黑色,描边没有应用。
我将矢量可绘制对象转换为位图,然后使用此函数应用边框,但它会全部填充为黑色,描边没有应用。
private static Bitmap getBitmap(VectorDrawable vectorDrawable)
{
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}
private static Bitmap getBitmap(Context context, int drawableId)
{
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable)
{
return ((BitmapDrawable) drawable).getBitmap();
}
else if (drawable instanceof VectorDrawable)
{
return getBitmap((VectorDrawable) drawable);
}
else
{
throw new IllegalArgumentException("unsupported drawable type");
}
}
private Bitmap addWhiteBorder(Bitmap bmp, int borderSize)
{
Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize*2 , bmp.getHeight() + borderSize*2 , bmp.getConfig());
Canvas canvas = new Canvas(bmpWithBorder);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, borderSize, borderSize, null);
return bmpWithBorder;
}