简体   繁体   English

有没有办法从大型图像创建 xxhdpi、xhdpi、hdpi、mdpi 和 ldpi 可绘制对象?

[英]Is there a way to create xxhdpi, xhdpi, hdpi, mdpi and ldpi drawables from a large scale image?

Is there a way to create xxhdpi, xhdpi, hdpi, mdpi and ldpi drawables from a large scale image automatically?有没有办法从大型图像自动创建 xxhdpi、xhdpi、hdpi、mdpi 和 ldpi 可绘制对象? For example assume that I have a 512x512 image and I want to have different versions of this images for different screen resolutions supported by Android in appropriate folders.例如,假设我有一个 512x512 的图像,并且我希望在适当的文件夹中为 Android 支持的不同屏幕分辨率拥有此图像的不同版本。

Option #1: Just ship the -xxhdpi drawables and let Android downsample them for you at runtime (downside: will only work on fairly recent devices, where -xxhdpi is known).选项#1:只需发送-xxhdpi可绘制对象,并让 Android 在运行时为您对它们进行下采样(缺点:仅适用于最近的设备,其中-xxhdpi已知)。

Option #2: Use Android Asset Studio to downsample them for you.选项 #2:使用Android Asset Studio为您降低采样率。

Option #3: Automate the process within a graphics editor, per ssantos' answer .选项 #3:根据ssantos 的回答,在图形编辑器中自动化该过程。

Option #4: Script yourself a solution, using something like ImageMagick.选项 #4:使用 ImageMagick 之类的工具为自己编写一个解决方案。

Option #5: Use image baker选项#5:使用图像烘焙器

Update :更新

The plugin previously mentioned has been abandoned, but it apparently has an up-to-date fork here .前面提到的插件已被放弃,但它显然有一个最新的分支here

Old Answer :旧答案

I use the Android Studio plugin named Android Drawable Importer :我使用名为Android Drawable Importer的 Android Studio 插件:

在此处输入图片说明

To use it after installed, right click your res/drawable folder and select New > Batch Drawable Import :要在安装后使用它,请右键单击您的 res/drawable 文件夹并选择New > Batch Drawable Import

Then select your image via the + button and set the Resolution to be xxhdpi (or whatever the resolution of your source image is).然后通过+按钮选择您的图像并将分辨率设置为 xxhdpi(或任何源图像的分辨率)。

Update:更新:

The old way of installing the plugin doesn't work anymore but a fork of original plugin is still functional here .安装插件的旧方法不再有效,但原始插件的分支在这里仍然有效。 You can still follow this answer after installing the plugin manually.手动安装插件后,您仍然可以遵循此答案。

If you want quick and easy way visit https://www.img-bak.in/ or https://appicon.co/ they work with iOS as well.如果您想要快速简便的方法,请访问https://www.img-bak.in/https://appicon.co/,它们也适用于 iOS。

I will try to explain the process in step wise basis, so that it will be simple to understand to anyone.我将尝试逐步解释该过程,以便任何人都可以轻松理解。

1. Install the plugin manually as provided in the ReadME 1.按照自述文件中的说明手动安装插件

2. Restart android studio 2.重启安卓工作室

3. As you can see in the following screencap, there is only one drawable here 3. 正如你在下面的屏幕截图中看到的,这里只有一个 drawable

在此处输入图片说明

4. Now right-click on the drawable folder and navigate to New>Batch Drawable Import 4. 现在右键单击 drawable 文件夹并导航到 New>Batch Drawable Import

在此处输入图片说明

8. 现在点击添加图标

5. Now select "the Single " image you want different variations of drawables. 5. 现在选择“单个”图像,您需要不同的可绘制变体。

在此处输入图片说明

6. Now select which dimension the origin image is. 6. 现在选择原始图像的尺寸。 If the origin image is xxhdpi like in my case select "xxhdpi " as "Source Resoultion".如果原始图像在我的情况下是 xxhdpi,请选择“xxhdpi”作为“源分辨率”。

7. Now press ok then ok again ..and then it will take few seconds and then you will magically get all the variables of the drawables. 7. 现在按 ok 然后再按 ok ..然后它会花几秒钟,然后你会神奇地获得可绘制对象的所有变量。 在此处输入图片说明

在此处输入图片说明

Use an online service like Image Baker .使用像Image Baker这样的在线服务。

图像烘焙师

It's simple.这很简单。 Upload the images and download processed assets for both Android and iOS.为 Android 和 iOS 上传图像并下载已处理的资产。

下载的图片

Note: Image Baker is a free service created by my friend and myself.注:Image Baker 是我和我的朋友创建的一项免费服务。

A bash script using ImageMagick (convert) as per CommonsWare's answer:根据 CommonsWare 的回答,使用 ImageMagick (convert) 的 bash 脚本:

Added folder creation and argument check thanks to Kishan Vaghela感谢Kishan Vaghela添加文件夹创建和参数检查

#!/bin/sh
#---------------------------------------------------------------
# Given an xxhdpi image or an App Icon (launcher), this script
# creates different dpis resources and the necessary folders
# if they don't exist
#
# Place this script, as well as the source image, inside res
# folder and execute it passing the image filename as argument
#
# Example:
# ./drawables_dpis_creation.sh ic_launcher.png
# OR
# ./drawables_dpis_creation.sh my_cool_xxhdpi_image.png
#
# Copyright (c) 2016 Ricardo Romao.
# This free software comes with ABSOLUTELY NO WARRANTY and
# is distributed under GNU GPL v3 license. 
#---------------------------------------------------------------

if [ $# -eq 0 ]; then
    echo "No arguments supplied"
else if [ -f "$1" ]; then
    echo " Creating different dimensions (dips) of "$1" ..."
    mkdir -p drawable-xxxhdpi
    mkdir -p drawable-xxhdpi
    mkdir -p drawable-xhdpi
    mkdir -p drawable-hdpi
    mkdir -p drawable-mdpi

    if [ $1 = "ic_launcher.png" ]; then
        echo "  App icon detected"
        convert ic_launcher.png -resize 144x144 drawable-xxhdpi/ic_launcher.png
        convert ic_launcher.png -resize 96x96 drawable-xhdpi/ic_launcher.png
        convert ic_launcher.png -resize 72x72 drawable-hdpi/ic_launcher.png
        convert ic_launcher.png -resize 48x48 drawable-mdpi/ic_launcher.png
        rm -i ic_launcher.png
    else
        convert $1 -resize 75% drawable-xxhdpi/$1
        convert $1 -resize 50% drawable-xhdpi/$1
        convert $1 -resize 38% drawable-hdpi/$1
        convert $1 -resize 25% drawable-mdpi/$1
        mv $1 drawable-xxxhdpi/$1
    fi
echo " Done"
else
    echo "$1 not found."
fi
fi

EDIT:编辑:

The website is now called appicon.co该网站现在称为appicon.co


I usually use assets.codly.io我通常使用assets.codly.io
It generates the assets locally in your browser, no upload, no download.它在您的浏览器中本地生成资产,无需上传,无需下载。

在此处输入图片说明

Just found an easy way to do it in the new Android Studio:刚刚在新的 Android Studio 中找到了一种简单的方法:

在此处输入图片说明

在此处输入图片说明

I use a tool called Android Icon Set in the Eclipse for standard icons like Launcher, ActionBar, Tab icons and notification icons.我在 Eclipse 中使用了一个名为 Android Icon Set 的工具来创建标准图标,例如启动器、操作栏、选项卡图标和通知图标。 You can launch it from File --> New --> Other.. --> Android --> Android Icon Set.您可以从文件 --> 新建 --> 其他.. --> Android --> Android 图标集启动它。 The best part is that you can choose any file from your computer and it will automatically place all the images of standard sizes into your project directory.最好的部分是您可以从计算机中选择任何文件,它会自动将所有标准尺寸的图像放入您的项目目录中。

在此处输入图片说明

  1. Just use https://romannurik.github.io/AndroidAssetStudio/index.html .只需使用https://romannurik.github.io/AndroidAssetStudio/index.html It can make a set of icons from an image, later you can download a zip-file.它可以从图像制作一组图标,稍后您可以下载一个 zip 文件。
  2. Or download a Windows application at https://github.com/redwarp/9-Patch-Resizer/releases (doesn't need to install) and open an icon.或者在https://github.com/redwarp/9-Patch-Resizer/releases下载 Windows 应用程序(不需要安装)并打开一个图标。
  3. Also you can use a plugin Android Drawable Importer , see answers above.您也可以使用插件Android Drawable Importer ,请参阅上面的答案。 Because it is abandoned, install forks.因为废弃,所以安装fork。 See Why does Android Drawable Importer ignore selection in AS 3.5 onwards or https://github.com/Vincent-Loi/android-drawable-importer-intellij-plugin .请参阅为什么 Android Drawable Importer 忽略 AS 3.5 以后的选择https://github.com/Vincent-Loi/android-drawable-importer-intellij-plugin
  4. https://appicon.co/#image-sets . https://appicon.co/#image-sets

I think that the tool provided by Redwarp is the best! 我认为Redwarp提供的工具是最好的! https://github.com/asystat/Final-Android-Resizer https://github.com/asystat/Final-Android-Resizer

Found it in this other question: https://stackoverflow.com/questions/10754523/is-there-any-tool-which-can-a-resize-png-image-into-a-image-for-ldpi-mdpi-hdp 在另一个问题中找到它: https//stackoverflow.com/questions/10754523/is-there-any-tool-which-can-a-resize-png-image-into-a-image-for-ldpi-mdpi -hdp

Since this one is the first result on Google, I wanted to add this info here too. 由于这是Google上的第一个结果,我想在此处添加此信息。

Use Android Studio Image Asset使用 Android Studio 图像资源

Go to:前往:

 Project>res --> right click

 new> image asset

Then set:然后设置:

-Icon type: Launcher Icons
-Asset type: Image
-Path: the/path/to/your/image
-Trim: No
-Padding: 0%
-Shape: None
-Effect: None

Select: Next>Finish.选择:下一步>完成。

Now you will have your icon in the correct resolutions.

视觉示例:

EDIT: I recommend to use SVG images to create Vector Drawables, and then use them in a canvas to resize them to the correct size or simply change the DP.编辑:我建议使用 SVG 图像来创建 Vector Drawables,然后在画布中使用它们将它们调整为正确的大小或简单地更改 DP。

You can get the default icons from Google or just create your Own您可以从 Google 获取默认图标,也可以创建自己的图标

 Project>res --> right click
 new> vector asset

Then set:然后设置:

-Asset type: Local file (SVG, PSD)
-Path: the/path/to/your/image
-Size: check Override to keep your aspect ratio.
-Chek enable auto mirroring for RTL Layout.

Select: Next>Finish.选择:下一步>完成。

Now you will have your icon and you will be able to change size, color etc.. .现在您将拥有您的图标,您将能够更改大小、颜色等...... 在此处输入图片说明

Not 100% automatic, but I save a lot of time using Photoshop Actions .不是 100% 自动,但我使用Photoshop Actions节省了很多时间。

For instance, given xhdpi assets, I then create a task for hdpi and mdpi , that scales to 66.66% and to 44.44% respectively.例如,给定xhdpi资产,然后我为hdpimdpi创建一个任务,分别扩展到 66.66% 和 44.44%。 Then I run the actions for all images on folder xhdpi .然后我对文件夹xhdpi上的所有图像运行操作。

For 512x512 images, all you have to do is calculate which percentage should you scale your images to achieve xxhpi, xhdpi, hdpi, and mdpi.对于 512x512 的图像,您所要做的就是计算应该缩放图像以达到 xxhpi、xhdpi、hdpi 和 mdpi 的百分比。

I wrote a Photoshop script to create ic_launcher png files from PSD file.我编写了一个 Photoshop 脚本来从 PSD 文件创建 ic_launcher png 文件。 Just check ic_launcher_exporter .只需检查ic_launcher_exporter

To use it, just download it and use the script from photoshop.要使用它,只需下载它并使用 photoshop 中的脚本。

在此处输入图片说明

And configure where you want to generate output files.并配置要生成输出文件的位置。

在此处输入图片说明

There is also the possibility to use the Vector Asset Studio in combination with Scalable Vector Graphics (SVG) .还可以将 Vector Asset Studio 与可缩放矢量图形 (SVG)结合使用。 Android Studio will handle the rest for you. Android Studio 将为您处理剩下的事情。 As the official documentation says:正如官方文档所说:

Vector Asset Studio helps you add material icons and import Scalable Vector Graphic (SVG) files into your app project as a drawable resource. Vector Asset Studio 可帮助您添加材质图标并将可缩放矢量图形 (SVG) 文件作为可绘制资源导入您的应用程序项目。 Compared to raster images, vector drawables can reduce the size of your app and be resized without loss of image quality.与光栅图像相比,矢量可绘制对象可以减小应用程序的大小并在不损失图像质量的情况下调整大小。 They help you to more easily support different Android devices with varying screen sizes and resolutions because you can display one vector drawable on all of them.它们帮助您更轻松地支持具有不同屏幕尺寸和分辨率的不同 Android 设备,因为您可以在所有设备上显示一个可绘制的矢量。

I consider this the future approach.我认为这是未来的方法。

Vector Asset Studio 的菜单项

Vector Asset Studio 的屏幕截图

The easiest way is to use Resource Manager最简单的方法是使用资源管理器

在此处输入图片说明

Then you can select each density然后你可以选择每个密度

在此处输入图片说明

And after importing you can see the 6 different versions of this image导入后,您可以看到此图像的 6 个不同版本

在此处输入图片说明

I've had luck using this plug-in for photoshop: 我很幸运使用这个插件用于photoshop:

check here 检查一下

There is some work to name layers and button states correctly. 有一些工作可以正确命名图层和按钮状态。 But, once you do that, the built in macros are pretty good. 但是,一旦你这样做,内置的宏是非常好的。

Otherwise, you could write your own macros in photoshop if you have the knowledge. 否则,如果您有知识,可以在photoshop中编写自己的宏。

I had using solution all this way in this thread, and it's easy working with plugin Android Drawable Importer我在这个线程中一直使用解决方案,并且使用插件Android Drawable Importer很容易

If u using Android Studio on MacOS, just try this step to get in:如果您在 MacOS 上使用 Android Studio,只需尝试此步骤即可进入:

  • Click bar menu Android Studio then choose Preferences or tap button Command + ,单击栏菜单Android Studio,然后选择首选项或点击按钮Command + ,
  • Then choose Plugins然后选择插件
  • Click Browse repositories单击浏览存储库
  • Write in the search coloumn Android Drawable Importer在搜索栏写Android Drawable Importer
  • Click Install button点击安装按钮
  • And then dialog Restart is showing, just restart it Android Studio然后对话框 Restart 显示,只需重新启动它 Android Studio

After ur success installing the plugin, to work it this plugin just click create New menu and then choose Batch Drawable Import .在您成功安装插件后,要使用此插件,只需单击 create New 菜单,然后选择Batch Drawable Import Then click plus button aka Add button, and go choose your file to make drawable.然后单击加号按钮又名添加按钮,然后选择您的文件以使其可绘制。 And then just click ok and ok the drawable has make it all of them.然后只需单击“确定”,“确定”即可绘制所有内容。

If u confused with my word, just see the image tutorial from learningmechine .如果你对我的话感到困惑,请参阅learningmechine的图像教程。

A simple plugin in Android will help you Step 1. Go to Settings Step 2. Click on Plugin Step 3. Search for Android Drawable Importer Step 4. Install plugin and restart Android 中的一个简单插件将帮助您 Step 1. 转到 Settings Step 2. 单击 Plugin Step 3. 搜索 Android Drawable Importer Step 4. 安装插件并重新启动

How to use?如何使用?

Go to File>New>Batch Drawable Import转到文件>新建>批量可绘制导入

Enjoy享受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM