Convert Android vector drawable to launcher icon PNG

前端 未结 1 546
鱼传尺愫
鱼传尺愫 2021-01-24 07:01

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.

Thi

1条回答
  •  清酒与你
    2021-01-24 07:13

    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
        //--------------------------------------------------------------------------------
    

    0 讨论(0)
提交回复
热议问题