将可绘制的Android矢量转换为启动器图标PNG [英] Convert Android vector drawable to launcher icon PNG

查看:100
本文介绍了将可绘制的Android矢量转换为启动器图标PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已将Android启动器图标设置为矢量,但发现应该使用PNG而不是vector(XML).如何将矢量图像转换为启动器图标的PNG.


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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8c-2.4,1.8 -4.8,3.7 -7.2,5.5c7.1,0.2 14.3,0.3 21.4,0.5c4.5,0.1 7,-3.5 7.5,-7.5c0.9,-7.1 1.8,-14.2 2.7,-21.3c-2.5,2.5 -5,5 -7.5,7.5c2,0 3.9,0 5.8,0c-2.5,-2.5 -5,-5 -7.5,-7.5c-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. 在ImageView中渲染矢量
  2. 从ImageView获取位图
  3. 将位图保存到PNG文件

在ImageView中渲染矢量:

render the Vector in an ImageView:

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

从ImageView获取位图:

get the Bitmap from the ImageView:

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

将位图保存到PNG文件:

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

这篇关于将可绘制的Android矢量转换为启动器图标PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆