如何获取Android Studio 1.5菜单图像代码作为PNG文件
我看到菜单图像文件是这样显示的如何获取Android Studio 1.5菜单图像代码作为PNG文件,android,image,android-studio,Android,Image,Android Studio,我看到菜单图像文件是这样显示的 <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0"> <path android:fillColor="#F
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0" />
<path
android:fillColor="#FF000000"
android:pathData="M9,2L7.17,4H4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2V6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2H9zm3,15c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z" />
</vector>
反之亦然。这个问题基本上是问是否可以将光栅图像(PNG)转换为矢量图形()。有很多工具可以做到这一点,但它们都有局限性 首先,您应该将PNG转换为SVG。我过去使用过ImageMagic,效果不错。有关更多选项和信息,请参阅以下两个StackOverflow帖子: 将PNG转换为SVG后,可以使用
如果要将Android转换为PNG,首先需要将drawable转换为SVG。为此,我编写了一个简单的命令行工具()。有了SVG之后,有很多工具可以将SVG转换为PNG(谷歌就是其中之一) 编辑:您还可以使用以下方法在Android上将VectorDrawable保存为PNG:
public static void vectorDrawableToPng(Context context, int drawableId, File file)
throws IOException {
final Drawable vectorDrawable = context.getResources().getDrawable(drawableId);
// Convert the VectorDrawable to a Bitmap
final Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
// Save the Bitmap as a PNG.
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}
我不建议将PNG转换为矢量,但上面的内容应该可以回答这个问题。当我在搜索更改菜单图标的方法时,我找到了这个网站 在那里你可以找到很多图标,也可以下载它们作为SVG甚至PNG
public static void vectorDrawableToPng(Context context, int drawableId, File file)
throws IOException {
final Drawable vectorDrawable = context.getResources().getDrawable(drawableId);
// Convert the VectorDrawable to a Bitmap
final Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
// Save the Bitmap as a PNG.
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
} finally {
if (fos != null) {
fos.close();
}
}
}