如何将Android XML Drawable保存,导出或转换为PNG图像文件?

时间:2022-05-26 06:45:49

I have a gradient I really like but I created it using XML drawable:

我有一个我非常喜欢的渐变,但我使用XML drawable创建它:

<gradient
    android:startColor="#DD181C18"
    android:endColor="#809C7D5A"
    android:angle="90"/>

What would be an easy way to create this to a png or something similar?

什么是一个简单的方法来创建一个png或类似的东西?

What I'm really looking for is: a tool that can and instructions using that tool to make gradients using an #aarrggbb format as it's input alpha/color (90 degree angle, angles in general, would be a plus).

我真正想要的是:一个工具可以和使用该工具的指令使用#aarrggbb格式制作渐变,因为它的输入alpha /颜色(90度角,一般角度,将是一个加号)。

Thanks for any help.

谢谢你的帮助。

EDIT: D'oh! I totally just realized ImageMagick should be able to do this. I will post the code that is my answer shortly, unless someone wants to beat me to it and receive the whole bounty!

编辑:D'哦!我完全意识到ImageMagick应该能够做到这一点。我会尽快发布我的答案代码,除非有人想要打败我并获得全部赏金!

4 个解决方案

#1


3  

Use a program like photoshop or paint.Net - they both have gradient tools and should let you set the colours in the same format as you have there.

使用像photoshop或paint.Net这样的程序 - 它们都有渐变工具,应该让你以与你在那里相同的格式设置颜色。

#2


3  

In the end it was a one liner using ImageMagick!

最后,它是使用ImageMagick的一个班轮!

I can't believe I didn't think to use ImageMagick before, Duh!!!

我不敢相信我之前没想过要使用ImageMagick,Duh !!!

convert -size 480x84 gradient:'rgba(156, 125, 90, 0.52)'-'rgba(24, 28, 24, 0.86)' tmp.png

For reference, I used this to create a .png image file using my Android XML drawable ##AARRGGBB values. ImageMagick FTW!!!

作为参考,我使用它来使用我的Android XML drawable ## AARRGGBB值创建.png图像文件。 ImageMagick FTW !!!

Thanks for everyone's input.

感谢大家的投入。

#3


1  

You could also create an activity with just the gradient as a background, and nothing else on the screen.

您还可以创建一个仅使用渐变作为背景的活动,而不是屏幕上的任何其他内容。

Run the app on an emulator or device, and use DDMS to take a screen capture. You will then have a nicely rendered PNG to save, and you can create whatever size you wish, by varying the screen size of the emulator you are using.

在模拟器或设备上运行应用程序,并使用DDMS进行屏幕捕获。然后,您将有一个很好的渲染PNG来保存,您可以通过改变您正在使用的模拟器的屏幕大小来创建您想要的任何大小。

Your XML could look like this:

您的XML可能如下所示:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/your_gradient">
</LinearLayout>

#4


1  

Give this a try:

尝试一下:

try {
    item.setDrawingCacheEnabled(true);
    Bitmap bmp = item.getDrawingCache();
    FileOutputStream out = new FileOutputStream(filename);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
    e.printStackTrace();
}

Here item is the view/view group that I want to render.

这里的item是我要渲染的视图/视图组。

I actually use the same fragment (minus the file write) for some on screen rendering and it seems preserve transparency.

我实际上在屏幕渲染上使用相同的片段(减去文件写入),它似乎保持透明度。

Note About Colors:

关于颜色的说明:

Colors can be specified in android using hexadecimal values. A byte (8 bits) are available for each color value in the pattern ARGB (total 32 bits). A stands for Alpha, R stands for Red, G for Green and B for Blue. Each component can have values between (0, 255). Note that:

可以使用十六进制值在android中指定颜色。一个字节(8位)可用于模式ARGB中的每个颜色值(总共32位)。 A代表Alpha,R代表红色,G代表绿色,B代表蓝色。每个组件的值可以在(0,255)之间。注意:

decimal 0   = hex 0x00
decimal 255 = hex 0xFF

So if you wanted a white color with opaque 'Alpha(no transparency) you would use#FFFFFFFF. For Black#FF000000. For Grey#FF808080.80is hex value of decimal 128 and gives you half of full intensity white. so80` gives you median Grey.

因此,如果你想要一个带有不透明'Alpha(没有透明度)的白色你会使用#FFFFFFF。对于Black#FF000000。对于灰色#FF808080.80是十进制128的十六进制值,并给出一半全强度白色。 so80`给你中值格雷。

Use Hexadecimal arithmetic to get values of your decimal colors.

使用十六进制算术来获取小数颜色的值。

AFAIK, Angle for a radial gradient should not make any difference. Try all of this in GIMP or read GIMP tutorials. Better still use Photoshop if you can (90 days Trial versions available).

AFAIK,径向渐变的角度应该没有任何区别。在GIMP中尝试所有这些或阅读GIMP教程。如果可以,最好还是使用Photoshop(90天试用版)。

#1


3  

Use a program like photoshop or paint.Net - they both have gradient tools and should let you set the colours in the same format as you have there.

使用像photoshop或paint.Net这样的程序 - 它们都有渐变工具,应该让你以与你在那里相同的格式设置颜色。

#2


3  

In the end it was a one liner using ImageMagick!

最后,它是使用ImageMagick的一个班轮!

I can't believe I didn't think to use ImageMagick before, Duh!!!

我不敢相信我之前没想过要使用ImageMagick,Duh !!!

convert -size 480x84 gradient:'rgba(156, 125, 90, 0.52)'-'rgba(24, 28, 24, 0.86)' tmp.png

For reference, I used this to create a .png image file using my Android XML drawable ##AARRGGBB values. ImageMagick FTW!!!

作为参考,我使用它来使用我的Android XML drawable ## AARRGGBB值创建.png图像文件。 ImageMagick FTW !!!

Thanks for everyone's input.

感谢大家的投入。

#3


1  

You could also create an activity with just the gradient as a background, and nothing else on the screen.

您还可以创建一个仅使用渐变作为背景的活动,而不是屏幕上的任何其他内容。

Run the app on an emulator or device, and use DDMS to take a screen capture. You will then have a nicely rendered PNG to save, and you can create whatever size you wish, by varying the screen size of the emulator you are using.

在模拟器或设备上运行应用程序,并使用DDMS进行屏幕捕获。然后,您将有一个很好的渲染PNG来保存,您可以通过改变您正在使用的模拟器的屏幕大小来创建您想要的任何大小。

Your XML could look like this:

您的XML可能如下所示:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/your_gradient">
</LinearLayout>

#4


1  

Give this a try:

尝试一下:

try {
    item.setDrawingCacheEnabled(true);
    Bitmap bmp = item.getDrawingCache();
    FileOutputStream out = new FileOutputStream(filename);
    bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
    e.printStackTrace();
}

Here item is the view/view group that I want to render.

这里的item是我要渲染的视图/视图组。

I actually use the same fragment (minus the file write) for some on screen rendering and it seems preserve transparency.

我实际上在屏幕渲染上使用相同的片段(减去文件写入),它似乎保持透明度。

Note About Colors:

关于颜色的说明:

Colors can be specified in android using hexadecimal values. A byte (8 bits) are available for each color value in the pattern ARGB (total 32 bits). A stands for Alpha, R stands for Red, G for Green and B for Blue. Each component can have values between (0, 255). Note that:

可以使用十六进制值在android中指定颜色。一个字节(8位)可用于模式ARGB中的每个颜色值(总共32位)。 A代表Alpha,R代表红色,G代表绿色,B代表蓝色。每个组件的值可以在(0,255)之间。注意:

decimal 0   = hex 0x00
decimal 255 = hex 0xFF

So if you wanted a white color with opaque 'Alpha(no transparency) you would use#FFFFFFFF. For Black#FF000000. For Grey#FF808080.80is hex value of decimal 128 and gives you half of full intensity white. so80` gives you median Grey.

因此,如果你想要一个带有不透明'Alpha(没有透明度)的白色你会使用#FFFFFFF。对于Black#FF000000。对于灰色#FF808080.80是十进制128的十六进制值,并给出一半全强度白色。 so80`给你中值格雷。

Use Hexadecimal arithmetic to get values of your decimal colors.

使用十六进制算术来获取小数颜色的值。

AFAIK, Angle for a radial gradient should not make any difference. Try all of this in GIMP or read GIMP tutorials. Better still use Photoshop if you can (90 days Trial versions available).

AFAIK,径向渐变的角度应该没有任何区别。在GIMP中尝试所有这些或阅读GIMP教程。如果可以,最好还是使用Photoshop(90天试用版)。