What is the best way to create drawables for different dpi

前端 未结 8 1268
难免孤独
难免孤独 2020-12-11 05:13

Do you create the MDPI drawable first and just scale it accordinly to the .075/1.00/1.50/2 ratio by pixels in photoshop or do you recreate each individual drawable?

8条回答
  •  醉酒成梦
    2020-12-11 05:46

    I suggest a little script write in powershell for Inkscape.

    Example :

    Put Inkscape in "c:\bin\inkscape" (dir without any space) and draw all your images in mdpi (1x) resolution.

    in Inkscape object properties box (i.e id in xml), give an Id name for each object that you would to export in png.

    Save your SVG to " C:\users\rone\Pictures\design-MyApps-forscript.svg"

    Create a dir "d:\temp".

    And put this script in "C:\app\scripts\"


    Powershell script name is "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
    

    now, call this script as this example :

    @("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 }

    And the script will create all your drawables in all resolutions (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi) in d:\temp\drawable-xyz ...

    So a confortable time saving.

提交回复
热议问题