Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

Is there a way to create xxhdpi, xhdpi, hdpi, mdpi and ldpi drawables from a large scale image automatically? 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.

like image 401
Ali Behzadian Nejad Avatar asked Oct 05 '13 10:10

Ali Behzadian Nejad


People also ask

What is LDPI and Mdpi?

For example, if you have a bitmap drawable that's 48x48 pixels for medium-density screen (the size for a launcher icon), all the different sizes should be: 36x36 for low-density (LDPI) 48x48 for medium-density (MDPI) 72x72 for high-density (HDPI)

What is Xxhdpi density?

xxhdpi device of 1080 x 1920 pixels (Samsung S4, S5) will be of 360 X 640 dp.


15 Answers

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).

Option #2: Use Android Asset Studio to downsample them for you.

Option #3: Automate the process within a graphics editor, per ssantos' answer.

Option #4: Script yourself a solution, using something like ImageMagick.

Option #5: Use image baker

like image 192
CommonsWare Avatar answered Oct 09 '22 14:10

CommonsWare


Update:

The plugin previously mentioned has been abandoned, but it apparently has an up-to-date fork here.

Old Answer:

I use the Android Studio plugin named Android Drawable Importer:

enter image description here

To use it after installed, right click your res/drawable folder and select 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).

like image 22
Adam Johns Avatar answered Oct 09 '22 14:10

Adam Johns


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.

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

2. Restart android studio

3. As you can see in the following screencap, there is only one drawable here

enter image description here

4. Now right-click on the drawable folder and navigate to New>Batch Drawable Import

enter image description here

8.  Now click on the add icon

5. Now select "the Single " image you want different variations of drawables.

enter image description here

6. Now select which dimension the origin image is. If the origin image is xxhdpi like in my case select "xxhdpi " as "Source Resoultion".

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.enter image description here

enter image description here

like image 28
erluxman Avatar answered Oct 09 '22 14:10

erluxman


Use an online service like Image Baker.

image baker

It's simple. Upload the images and download processed assets for both Android and iOS.

Downloaded images

Note: Image Baker is a free service created by my friend and myself.

like image 25
Asim K T Avatar answered Oct 09 '22 14:10

Asim K T


A bash script using ImageMagick (convert) as per CommonsWare's answer:

Added folder creation and argument check thanks to 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
like image 26
rdromao Avatar answered Oct 09 '22 15:10

rdromao


EDIT:

The website is now called appicon.co


I usually use assets.codly.io
It generates the assets locally in your browser, no upload, no download.

enter image description here

like image 31
A.Zidan Avatar answered Oct 09 '22 16:10

A.Zidan


Just found an easy way to do it in the new Android Studio:

enter image description here

enter image description here

like image 22
Kohn1001 Avatar answered Oct 09 '22 14:10

Kohn1001


I use a tool called Android Icon Set in the Eclipse for standard icons like Launcher, ActionBar, Tab icons and notification icons. You can launch it from File --> New --> Other.. --> Android --> Android Icon Set. 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.

enter image description here

like image 38
Nitin Sethi Avatar answered Oct 09 '22 16:10

Nitin Sethi


  1. Just use https://romannurik.github.io/AndroidAssetStudio/index.html. It can make a set of icons from an image, later you can download a zip-file.
  2. Or download a Windows application at https://github.com/redwarp/9-Patch-Resizer/releases (doesn't need to install) and open an icon.
  3. Also you can use a plugin Android Drawable Importer, see answers above. Because it is abandoned, install forks. See Why does Android Drawable Importer ignore selection in AS 3.5 onwards or https://github.com/Vincent-Loi/android-drawable-importer-intellij-plugin.
  4. https://appicon.co/#image-sets.
like image 41
CoolMind Avatar answered Oct 09 '22 14:10

CoolMind


Use Android Studio Image Asset

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.

Visual Example:

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.

You can get the default icons from Google or just create your Own

 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.. . enter image description here

like image 42
CarLoOSX Avatar answered Oct 09 '22 14:10

CarLoOSX


Not 100% automatic, but I save a lot of time using 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. Then I run the actions for all images on folder 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.

like image 31
ssantos Avatar answered Oct 09 '22 16:10

ssantos


There is also the possibility to use the Vector Asset Studio in combination with Scalable Vector Graphics (SVG). Android Studio will handle the rest for you. 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. 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.

I consider this the future approach.

Menu entry of the Vector Asset Studio

Screenshot of the Vector Asset Studio

like image 44
Paul Spiesberger Avatar answered Oct 09 '22 16:10

Paul Spiesberger


The easiest way is to use Resource Manager

enter image description here

Then you can select each density

enter image description here

And after importing you can see the 6 different versions of this image

enter image description here

like image 39
Gastón Saillén Avatar answered Oct 09 '22 14:10

Gastón Saillén


Option #5: Use image baker is flaky. I convert two images and it stopped. I managed to convert 10 images with: https://nsimage.brosteins.com/

like image 20
MeLean Avatar answered Oct 09 '22 15:10

MeLean


I wrote a Photoshop script to create ic_launcher png files from PSD file. Just check ic_launcher_exporter.

To use it, just download it and use the script from photoshop.

enter image description here

And configure where you want to generate output files.

enter image description here

like image 42
xcesco Avatar answered Oct 09 '22 15:10

xcesco