用于导出画板中每个图层的所有 Android 相关分辨率的脚本

Script to export all Android-relevant resolutions for each layer in an artboard

上下文

为了 Android 开发,我正在尝试为 Illustrator 创建一个脚本,将我的矢量作品从每一层导出为 .png 格式,因此每次导出都必须以所有密度提供(ppi)-格式 Android 需要。我知道我可以像这样导出图像:

png24_settings = function ( ) {
    var options = new ExportOptionsPNG24();
    options.antiAliasing = true;
    options... = ...
    ...
    return options;
}

var file_destination = "Some string with the destination path and name"
var doc = app.activeDocument; //Retrieve active document
doc.exportFile(file_destination, ExportType.PNG24, png24_settings() ); // Exports visible canvas

每一层都可以通过遍历图层并将所有其他图层设置为不可见来导出。 但是,我无法使用给定的函数创建一个循环来帮助我解决密度问题。

问题

exportFile()-func 不采用任何分辨率参数,因此不能用于创建 Android(hdpi、xhdpi、...)所需的不同密度文件。 我必须选择哪个选项才能以所有必要的 ppi 格式导出每个单层?

通过查阅文档,我解决了我的问题。 存在一个名为 imageCapture() 的函数,其中包含用于所需 ppi (link to docs) 的 resolution 参数。请注意,此功能将导出画板上的所有可见层。因此,导出的迭代需要注意可见性。

有了这些信息,我可以遍历画板中的每一层并进行导出。 该脚本类似于下面提供的脚本。我知道它有点详尽,但我希望它有助于清楚地理解。

提供设置信息

// Provide density-information necessary for android
android_densities = [
      120 // ldpi
    , 160 // mdpi
    , 240 // hdpi
    , 320 // xhdpi
    , 480 // xxhdpi
    , 640 // xxxhdpi
]

// Provide settings for image export which takes the respective density as argument
setOptions = function(density) {
    var options = new ImageCaptureOptions();
   
    options.artBoardClipping = true;
    options.resolution       = density // Your desired ppi
    options...                         // Provide other options you'd also like to implement.
    
    return options;
}


// Initialize current document as document to work with
var doc = app.activeDocument; //Gets the active document

// Retrieve current directory of illustrator-document as default path to save to
// If you want to save anywhere else, just provide it here
var filePath = (app.activeDocument.fullName.parent.fsName).toString().replace(/\/g, '/');

// Select current artboard according to current document
// This is the artboard you want to export your layers from
var activeAB = doc.artboards[doc.artboards.getActiveArtboardIndex()];

注意: Android-密度在文档中提供 here

迭代导出

// Export, for example, all visible layers.
// Thus, all layers are first iterated, checked for visibility and set invisible.
// Because of the last step all elements have to be cached in an array (= export_layers)
export_layers= []; // Store layers to export
for(var i=0; i<doc.layers.length; i++){
    var layer = doc.layers[i];
    if(layer.visible){
        export_layers.push(i);
        doc.layers[i].visible=false;
    };
}

// Do exports
for(var exportElement=0; exportElement<export_layers.length; exportElement++) {
    
    // Get layer to export
    var layer = doc.layers[export_layers[exportElement]];
    
    // Set this layer visible (it is the only layer visible now, because imageCapture will export all visible layers)
    doc.layers[export_layers[exportElement]].visible=true;
    
    // Iterate over all densities
    for(var densityElement=0; densityElement<android_densities.length; densityElement++) {
        
        // Provide path and name for output image
        var destination_of_file = new File(filePath + "/" + layer.name + "_" + String(android_densities[densityElement]) + ".png");
        
        // Save file with resolution
        doc.imageCapture(destination_of_file, activeAB.artboardRect, setOptions(android_densities[densityElement]));
    }

    // Set visibility to false again for next layer to be exported correctly
    doc.layers[export_layers[exportElement]].visible=false;
}


// Finally, reset the visibility-status prior to the export
for(var resetElement=0; i<export_layers.length; resetElement++) {
    doc.layers[export_layers[resetElement]].visible=true;
}

之后,您应该在指定文件夹中拥有所有密度的所有图层。当然,通过为每个分辨率提供一个文件夹等等,这可以更加优雅 - 我只是尽量保持答案的重点:)