Last active
November 29, 2024 02:28
-
-
Save thuanpham582002/a0fde376b68387e5fdd13cbd79ae1d9d to your computer and use it in GitHub Desktop.
SDP, SSP Gradle Script, Built on sdp of intuit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class SDPFactory : DefaultTask() { | |
companion object { | |
private const val MIN_DPI = 300 | |
private const val MAX_DPI = 1080 | |
private const val DPI_STEP = 30 | |
} | |
@Input | |
var positiveMax = 600 | |
@Input | |
var negativeMax = 60 | |
@Input | |
var defaultDpi = 390 | |
@TaskAction | |
fun create() { | |
val resFolder = File(project.projectDir, "src/main/res") | |
for (dpi in MIN_DPI..MAX_DPI step DPI_STEP) { | |
val folder = File(resFolder, "values-sw${dpi}dp") | |
if (!folder.exists()) { | |
folder.mkdir() | |
} | |
createFile(folder, "positive_sdps.xml", dpi, isNegative = false) | |
createFile(folder, "negative_sdps.xml", dpi, isNegative = true) | |
createFile(folder, "positive_ssps.xml", dpi, isNegative = false, unit = "sp") | |
createFile(folder, "negative_ssps.xml", dpi, isNegative = true, unit = "sp") | |
} | |
} | |
private fun createFile( | |
folder: File, | |
fileName: String, | |
dpi: Int, | |
isNegative: Boolean, | |
unit: String = "dp") { | |
val file = File(folder, fileName) | |
if (!file.exists()) { | |
file.createNewFile() | |
} | |
PrintWriter(file).use { printerWriter -> | |
printerWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>") | |
printerWriter.println("<resources>") | |
val max = if (isNegative) negativeMax else positiveMax | |
for (i in 1..max) { | |
val dimensionValue = String.format("%.2f", i * dpi / defaultDpi.toFloat()) | |
val dimenName = if (isNegative) "_minus${i}s$unit" else "_${i}sdp" | |
printerWriter.println("<dimen name=\"$dimenName\">${if (isNegative) "-$dimensionValue" else dimensionValue}$unit</dimen>") | |
} | |
printerWriter.println("</resources>") | |
} | |
} | |
} | |
tasks.register<SDPFactory>("createSDP") |
To run, just click double ctrl and type createSDP, then click enter.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The default DPI is 390 for fitting designs in Figma, but I'm exhausted from constantly recalculating dp from another library that uses 300 DPI.