5

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:

enter image description here

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;
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
waqas
  • 51
  • 1
  • 3

2 Answers2

0

Assume you have the VectorDrawable defined in vectordrawable.xml

<vector
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100"
    android:viewportHeight="100">
    <path
        android:name="headset"
        android:strokeColor="#FF000000"
        android:strokeWidth="0"
        ...
        android:pathData="..." />
</vector>

Then you can define an AnimatedVectorDrawable to change the strokeWidth

<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vectordrawable">
    <target
        android:animation="@animator/change_stroke_width"
        android:name="headset" />

</animated-vector>

Finally, define the animation that changes the strokeWidth in change_stroke_width.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <objectAnimator
        android:duration="100"
        android:propertyName="strokeWidth"
        android:valueFrom="0"
        android:valueTo="10" />
</set>
Doris Liu
  • 807
  • 5
  • 9
  • i assume this will draw a rectangle border around the bitmap, whereas i want to add the stroke/border to the path of vector drawable (or the bitmap created from it) @Doris – waqas Nov 22 '16 at 07:09
  • i added the image in question please see @Doris – waqas Nov 22 '16 at 07:13
  • Yes, the answer above would produce a rectangular border. After seeing the image you added, I assume this VectorDrawable can be represented with one path, in which case you would need to define strokeWidth and strokeColor to achieve that black outline/border. – Doris Liu Nov 22 '16 at 07:25
  • Is the strokeWidth determined programmatically? @waqas – Doris Liu Nov 22 '16 at 07:27
  • no the stroke and width on the image is not added programatically, i added in the vector drawables xml , but i want same kind of result programatically ,could u please help. @Doris – waqas Nov 22 '16 at 07:44
  • VectorDrawable doesn't expose the Path/Group properties through Java APIs. In other words, you won't be able to change strokeWidth through Java public API. If it's a fixed strokeWidth, you could consider using AnimatedVectorDrawable to change/animate the strokeWidth from 0 to the pre-defined value. Let me know if you need an example of such an AnimatedVectorDrawable. @waqas – Doris Liu Nov 22 '16 at 08:04
  • Please if u could provide an example of this it would be really helpful, i'm stuck at this from a week :( @Doris – waqas Nov 22 '16 at 08:16
  • Ok thanks for the nice explanation :) i need to know how to apply this, i want the stroke width to be applied on seekbarvalue, means i increase the seek the stroke increases @Doris – waqas Nov 22 '16 at 09:39
  • How to draw border around ? – K Pradeep Kumar Reddy Aug 14 '21 at 16:56
0

Look at this answer

https://stackoverflow.com/a/52960168/10592895

You can use vector drawable with different viewport width and height as background.

To do it programmatically, you can set background to transparent or a drawable with different dimension based on your needs.

Hope this helps.