вторник, 18 июня 2013 г.

Android Assets Generator

For Android app or game you need graphics and all images must be for varius densities. There are:
Small ldpi        ~10% (240x320)     0.75  2"-3.5"     120dpi
Normal mdpi   ~16%  (320x480)    1.0    3.6"-4.9"  160dpi 
Normal hdpi    ~37%  (480x800)    1.5    3.6"-4.9"  240dpi
Normal xhdpi  ~25% (720x1280)    2.0   3.6"-4.9"  320dpi
Large  xxhdpi              (1080x1920) 3.0        5"        480dpi

So you need graphics for 5 densities : 120 , 160 , 240 , 320 and 480 dpi.
Manually doing this is nightmare but I found very useful script for generating Android graphics automatically though Adobe Photoshop scripting. It generates all assets from XXHDPI source , so you will get the best possible quality for all densities. I suggest you to make graphics in Adobe Illustrator then export it to Photoshop and make size which you need for 480dpi , or if you don't know it size you can make an image for 1.0 scale factor (320x480) then multiply to 3.0 and so you know the size you need for XXHDPI image.

Then the following steps are required:
1. Make directory C:/ANDROID_IMAGES (I have slighty modified script to point to this folder because there can be problems with various names of folders , if you have problems with UAC then you can change in script to any drive letter you want)
2. Place your XXHDPI image into this folder
3. Run script

Thats all , you will get all android images for all densities sorted into folders.
So here is script , you must save this into file with extension .jsx :


// Output Android Assets.jsx
// 2012 Todd Linkner
// License: none (public domain)
// v1.0
//
// This scrip is for Photoshop CS6. It outputs Android XHDPI, HDPI, MDPI,
// and LDPI PNG assets from XXHDPI source files. The resulting PNGs will be
// placed in sub-folders within your target folder.

/*
// BEGIN__HARVEST_EXCEPTION_ZSTRING
<javascriptresource>
<name>$$$/JavaScripts/OutputAndroidAssets/MenuAlt=Output Android Assets</name>
<category>mobile</category>
</javascriptresource>
// END__HARVEST_EXCEPTION_ZSTRING
*/

// bring Photoshop into focus
#target photoshop

main();

function main() {

    var cleanup = confirm("This script outputs Android XHDPI, HDPI, MDPI, "
                        + "and LDPI PNG assets from HDPI source files.\r\r"
                        + "Do you want to delete your original files when "
                        + "complete?");
 
    // Ask user for input folder
//var inputFolder = Folder.selectDialog("Select a folder to process");
//if (inputFolder == null) throw "No folder selected. Exting script.";
    var inputFolder = Folder ("C:/ANDROID_IMAGES")

// get all files in the input folder
var fileList = inputFolder.getFiles("*.png");

// Make output folders
var dirxhdpi = Folder(inputFolder+"/drawable-xhdpi");
if(!dirxhdpi.exists) dirxhdpi.create();
var dirhdpi = Folder(inputFolder+"/drawable-hdpi");
if(!dirhdpi.exists) dirhdpi.create();
var dirmdpi = Folder(inputFolder+"/drawable-mdpi");
if(!dirmdpi.exists) dirmdpi.create();
var dirldpi = Folder(inputFolder+"/drawable-ldpi");
if(!dirldpi.exists) dirldpi.create();



// Open each file in turn
for (var i = 0; i < fileList.length; i++) {
// Open file
open(fileList[i]);
// Make XHDPI
resize(dirxhdpi,'66.7%');
// Make HDPI
resize(dirhdpi,'50%');
// Make MDPI
resize(dirmdpi,'33.3%');
// Close and do not save
// Make LDPI
resize(dirldpi,'25%');
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
// Delete the original
if (cleanup) fileList[i].remove();
}
    alert("Done!");
}

function resize(dir,percent) {
    // Setup file name
    var fname = app.activeDocument.name.replace(/\s+/g, '_').replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase();

    // Set export options
    var opts, file;
    opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.PNG;
    opts.PNG8 = false;
    opts.transparency = true;
    opts.interlaced = 0;
    opts.includeProfile = false;
    opts.optimized = true;

    // Duplicate, resize and export
    var tempfile = app.activeDocument.duplicate();
    if (undefined != percent) tempfile.resizeImage(percent,percent);
    file = new File(dir+"/"+fname);
    tempfile.exportDocument(file, ExportType.SAVEFORWEB, opts);
    tempfile.close(SaveOptions.DONOTSAVECHANGES);
}

Комментариев нет:

Отправить комментарий

Zen Stories

Simple application contains zen stories . Feel the wisdom of teachers of the past. Zen is a school of Mahayana Buddhism that developed in Ch...