我建议在 Powershell 中为 Inkscape 编写一个小脚本。
示例:
将 Inkscape 放入“c:\bin\inkscape”(没有任何空格的目录)并以 mdpi (1x) 分辨率绘制所有图像。
在 Inkscape 对象属性框中(即 xml 中的 id),为要导出为 png 的每个对象指定一个 ID 名称。
将您的 SVG 保存到“C:\users\rone\Pictures\design-MyApps-forscript.svg”
创建一个目录“d:\temp”。
并将这个脚本放在“C:\app\scripts\”中
Powershell 脚本名称是“inkscape_to_png.ps1”:
param (
$inkscape_dir="C:\bin\Inkscape\",
$inkscape_bin="inkscape.exe",
$img_id="",
$fichier_svg="C:\Users\rone\Pictures\design-MyMap-forscript.svg",
$tmp_dir="d:\temp\"
)
$inkscape=$(Resolve-Path "$inkscape_dir\$inkscape_bin")
function getWidthHeight($img_id) {
$size=@{}
$old_pwd=$pwd.path
cd $inkscape_dir
write-host -foreground yellow "détermine la taille de $img_id"
$size.width=invoke-command {./inkscape --file=$fichier_svg --query-id=$img_id --query-width 2>$null}
$size.height=invoke-command {./inkscape --file=$fichier_svg --query-id=$img_id --query-height 2>$null}
write-host -foreground yellow "width : $($size.width)"
write-host -foreground yellow "height : $($size.height)"
cd $old_pwd
return $size
}
function convertTo($size, $format) {
$rsize=@{}
if ($format -eq "MDPI") {
$rsize.width=[int]$size.width*1
$rsize.height=[int]$size.height*1
} elseif ($format -eq "LDPI") {
$rsize.width=[int]$size.width*0.75
$rsize.height=[int]$size.height*0.75
} elseif ($format -eq "HDPI") {
$rsize.width=[int]$size.width*1.5
$rsize.height=[int]$size.height*1.5
} elseif ($format -eq "XHDPI") {
$rsize.width=[int]$size.width*2
$rsize.height=[int]$size.height*2
} elseif ($format -eq "XXHDPI") {
$rsize.width=[int]$size.width*3
$rsize.height=[int]$size.height*3
} elseif ($format -eq "XXXHDPI") {
$rsize.width=[int]$size.width*4
$rsize.height=[int]$size.height*4
}
write-host -foreground yellow "après conversion : $format"
write-host -foreground yellow "width : $($rsize.width)"
write-host -foreground yellow "height : $($rsize.height)"
return $rsize
}
function inkscape_convert() {
$mdpi_size=getWidthHeight $img_id
write-host -foreground gray "----------------------------------------"
"MDPI,LDPI,HDPI,XHDPI,XXHDPI,XXXHDPI" -split ","|% {
$new_size=convertTo $mdpi_size $_
if ($new_size.width -eq 0 -or $new_size.height -eq 0) {
write-host -foreground red "erreur lors de la recherche de la taille de l'image"
exit
}
$w=$new_size.width
$h=$new_size.height
$dir="$tmp_dir\drawable-$($_.toLower())"
if (-not $(test-path $dir)) {
write-host -foreground yellow "création du répertoire $dir"
mkdir $dir
}
$new_file_name="$dir\$($img_id).png"
$cmd="$inkscape -z -i $img_id -j -f $fichier_svg -w $w -h $h -e $new_file_name"
write-host -foreground gray $cmd
invoke-expression -command $cmd
if ($? -eq $true) {
write-host -foreground yellow "conversion OK"
}
}
write-host -foreground gray "----------------------------------------"
}
inkscape_convert
现在,调用这个脚本作为这个例子:
@("btn_button_name_1","btn_button_name_3","btn_other", "btn_zoom", "btn_dezoom", "btn_center", "btn_wouwou", "im_abcdef", "ic_half", "ic_star", "ic_full") | % { C:\app\scripts\inkscape_to_png.ps1 -img $_ -f design-MyMap-forscript.svg }
脚本将在 d:\temp\drawable-xyz ... 中创建所有分辨率(ldpi、mdpi、hdpi、xhdpi、xxhdpi、xxxhdpi)的所有可绘制对象...
这样可以节省时间。