I can change the color of vector drawables programmatically, but I want to apply the stroke to vector drawable. I need a method that will change the vector drawable stroke at runtime:
previously i used this method but failed in my case.
i converted the Vector drawable into bitmap and then apply border with this function but it fills all with black, the stroke is not applied.
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;
}