ScaleAndroidIcons.ps1 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Scale Android Icons to Multiple Densities
  2. # This script takes a source icon and scales it to all required Android densities
  3. param(
  4. [string]$SourceIcon = "..\Platforms\Android\Resources\drawable\icon.png",
  5. [string]$OutputBase = "..\Platforms\Android\Resources"
  6. )
  7. Add-Type -AssemblyName System.Drawing
  8. # Define Android density sizes for launcher icons
  9. $densities = @{
  10. "drawable-mdpi" = 48
  11. "drawable-hdpi" = 72
  12. "drawable-xhdpi" = 96
  13. "drawable-xxhdpi" = 144
  14. "drawable-xxxhdpi" = 192
  15. }
  16. # Resolve paths
  17. $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  18. $sourcePath = Join-Path $scriptDir $SourceIcon
  19. $outputBasePath = Join-Path $scriptDir $OutputBase
  20. Write-Host "Source icon: $sourcePath" -ForegroundColor Cyan
  21. Write-Host "Output base: $outputBasePath" -ForegroundColor Cyan
  22. Write-Host ""
  23. if (-not (Test-Path $sourcePath)) {
  24. Write-Host "Error: Source icon not found at $sourcePath" -ForegroundColor Red
  25. exit 1
  26. }
  27. # Load source image
  28. $sourceImage = [System.Drawing.Image]::FromFile($sourcePath)
  29. Write-Host "Source image size: $($sourceImage.Width)x$($sourceImage.Height)" -ForegroundColor Yellow
  30. # Check if image is square
  31. if ($sourceImage.Width -ne $sourceImage.Height) {
  32. Write-Host "Warning: Source image is not square. Will scale to fit within square bounds." -ForegroundColor Yellow
  33. $maxDimension = [Math]::Max($sourceImage.Width, $sourceImage.Height)
  34. } else {
  35. $maxDimension = $sourceImage.Width
  36. }
  37. Write-Host ""
  38. # Process each density
  39. foreach ($density in $densities.GetEnumerator()) {
  40. $targetSize = $density.Value
  41. $outputDir = Join-Path $outputBasePath $density.Key
  42. $outputPath = Join-Path $outputDir "icon.png"
  43. # Create directory if it doesn't exist
  44. if (-not (Test-Path $outputDir)) {
  45. New-Item -Path $outputDir -ItemType Directory -Force | Out-Null
  46. }
  47. Write-Host "Scaling to $($density.Key): ${targetSize}x${targetSize} px" -ForegroundColor Green
  48. # Create new bitmap
  49. $newBitmap = New-Object System.Drawing.Bitmap($targetSize, $targetSize)
  50. $graphics = [System.Drawing.Graphics]::FromImage($newBitmap)
  51. # Set high quality rendering
  52. $graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
  53. $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
  54. $graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality
  55. $graphics.CompositingQuality = [System.Drawing.Drawing2D.CompositingQuality]::HighQuality
  56. # Calculate scaling to fit within target size (maintain aspect ratio)
  57. $scaleWidth = $targetSize / $sourceImage.Width
  58. $scaleHeight = $targetSize / $sourceImage.Height
  59. $scale = [Math]::Min($scaleWidth, $scaleHeight)
  60. $newWidth = [int]($sourceImage.Width * $scale)
  61. $newHeight = [int]($sourceImage.Height * $scale)
  62. # Center the image
  63. $x = [int](($targetSize - $newWidth) / 2)
  64. $y = [int](($targetSize - $newHeight) / 2)
  65. # Clear background (transparent)
  66. $graphics.Clear([System.Drawing.Color]::Transparent)
  67. # Draw the scaled image
  68. $destRect = New-Object System.Drawing.Rectangle($x, $y, $newWidth, $newHeight)
  69. $srcRect = New-Object System.Drawing.Rectangle(0, 0, $sourceImage.Width, $sourceImage.Height)
  70. $graphics.DrawImage($sourceImage, $destRect, $srcRect, [System.Drawing.GraphicsUnit]::Pixel)
  71. # Save the image
  72. $newBitmap.Save($outputPath, [System.Drawing.Imaging.ImageFormat]::Png)
  73. # Cleanup
  74. $graphics.Dispose()
  75. $newBitmap.Dispose()
  76. Write-Host " -> Saved to: $outputPath" -ForegroundColor Gray
  77. }
  78. # Cleanup source image
  79. $sourceImage.Dispose()
  80. Write-Host ""
  81. Write-Host "Icon scaling complete!" -ForegroundColor Green
  82. Write-Host "All density icons have been generated." -ForegroundColor Green