Convert Android vector drawable to launcher icon PNG

4.9k views Asked by At

I've made Android launcher icon as vector but I found out that I should use PNGs instead of vector(XML). How can I convert my vector image to PNGs for launcher icon.

This is vector if it's of any help:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="oval">
            <solid
                android:color="#5FB0C9"/>
            <size
                android:width="100dp"
                android:height="100dp"/>
            <stroke
                android:width="5dip"
                android:color="#3DA8C9"/>
        </shape>
    </item>
    <item
        android:gravity="center">
        <vector
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:width="60dp"
            android:height="60dp"
            android:viewportHeight="100.0"
            android:viewportWidth="100.0">
            <path
                android:fillColor="#2c819b"
                android:pathData="M72.4,3.5c-2,0 -3.9,0 -5.9,0c-4.5,0 -7,3.4 -7.5,7.5c-0.9,7.3 -1.8,14.6 -2.7,21.9c2.5,-2.5 5,-5 7.5,-7.5c-7.8,-0.2 -15.6,-0.3 -23.3,-0.5c2.5,2.5 5,5 7.5,7.5c0.9,-7.1 1.8,-14.3 2.7,-21.4c0.5,-4.1 -3.8,-7.5 -7.5,-7.5c-2,0 -3.9,0 -5.8,0c-4.5,0 -7,3.4 -7.5,7.5c-0.9,7.1 -1.8,14.2 -2.7,21.3c2.5,-2.5 5,-5 7.5,-7.5c-7.1,-0.2 -14.3,-0.3 -21.4,-0.5c-3,-0.1 -6.8,2.3 -7.2,5.5c-0.3,1.9 -0.6,3.9 -0.8,5.8c-0.7,4.8 1.9,9.4 7.2,9.5c7.2,0.2 14.4,0.3 21.5,0.5c-2.5,-2.5 -5,-5 -7.5,-7.5c-1,7.7 -1.9,15.4 -2.9,23.2c2.5,-2.5 5,-5 7.5,-7.5c-7.3,-0.2 -14.7,-0.3 -22,-0.5c-3,-0.1 -6.8,2.3 -7.2,5.5c-0.3,1.9 -0.6,3.9 -0.8,5.8c-0.7,4.8 1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c-0.9,7.1 -1.8,14.3 -2.7,21.4c-0.5,4.1 3.8,7.4 7.5,7.5c7.8,0.2 15.6,0.3 23.3,0.5c4.5,0.1 7,-3.5 7.5,-7.5c0.9,-7.3 1.8,-14.6 2.7,-21.9c-2.5,2.5 -5,5 -7.5,7.5c2,0 3.9,0 5.9,0C82.1,18.5 82.1,3.5 72.4,3.5z"/>
            <path
                android:fillColor="#2c819b"
                android:pathData="M60.2,54.3c-7.8,-0.2 -15.6,-0.3 -23.3,-0.5c2.5,2.5 5,5 7.5,7.5c1,-7.7 1.9,-15.4 2.9,-23.2c-2.5,2.5 -5,5 -7.5,7.5c7.8,0.2 15.6,0.3 23.3,0.5c-2.5,-2.5 -5,-5 -7.5,-7.5c-1,7.7 -1.9,15.4 -2.9,23.2c-0.5,4.1 3.8,7.5 7.5,7.5c4.5,0 7,-3.4 7.5,-7.5c1,-7.7 1.9,-15.4 2.9,-23.2c0.5,-4.1 -3.8,-7.4 -7.5,-7.5c-7.8,-0.2 -15.6,-0.3 -23.3,-0.5c-4.5,-0.1 -7,3.5 -7.5,7.5c-1,7.7 -1.9,15.4 -2.9,23.2c-0.5,4.1 3.8,7.4 7.5,7.5c7.8,0.2 15.6,0.3 23.3,0.5C69.9,69.5 69.9,54.5 60.2,54.3z"/>
        </vector>
    </item>
</layer-list>
1

There are 1 answers

4
Jon Goodwin On

All you need to do is:

  1. Render the Vector in an ImageView
  2. Get the Bitmap from the ImageView
  3. Save the Bitmap to a PNG file

render the Vector in an ImageView:

//--------------------------------------------------------------------------------
    ImageView image = R.findViewById(R.id.imageView);//get ImageView
//--------------------------------------------------------------------------------

get the Bitmap from the ImageView:

//--------------------------------------------------------------------------------
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();//get Bitmap
    SaveImage(bitmap,"my_png_file");//save it
//--------------------------------------------------------------------------------

Save the Bitmap to a PNG file:

//--------------------------------------------------------------------------------
    private void SaveImage(Bitmap finalBitmap, String filename)
    {
           String root = Environment.getExternalStorageDirectory().toString();
           File myDir = new File(root + "/saved_images");
           myDir.mkdirs();
           Random generator = new Random();
           int n = 10000;
           n = generator.nextInt(n);

           String fname = filename +".png";
           File file = new File (myDir, fname);
           if (file.exists ()) file.delete ();
           try {
               FileOutputStream out = new FileOutputStream(file);
               finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
               out.flush();
               out.close();
               sendBroadcast(new Intent(
                   Intent.ACTION_MEDIA_MOUNTED,
                   Uri.parse("file://" + Environment.getExternalStorageDirectory())));

           } catch (Exception e) {
               e.printStackTrace();
           }
    }//SaveImage
    //--------------------------------------------------------------------------------