I'm using Android Studio
with Gradle
.
I wonder whether there is any way to automate image resizing for different resolutions at build time. I mean to have one set of the images and to resize automatically to various resolutions (e.g. xxhdpi
, xhdpi
, hdpi
, mdpi
, ldpi
)
Perfect solution would be to have some gradle plugin which can be configured to produce resized images in appropriate folder before building apk
I developed a gradle plugin that allow doing exactly that (based on imagemagick)
First, you need to add the plugin to the build script :
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.eowise:gradle-imagemagick:0.4.0'
}
}
Then, add a task for each size :
[
[ variant: 'mdpi', size: '33%' ],
[ variant: 'hdpi', size: '50%' ],
[ variant: 'xhdpi', size: '66%' ],
[ variant: 'xxhdpi', size: '100%' ]
].each {
item ->
task("buildIcons${item.variant.capitalize()}", type: com.eowise.imagemagick.tasks.Magick) {
convert 'resources/images', { include '*.png' }
into "app/src/main/res/drawable-${item.variant}"
actions {
-background('none')
inputFile()
-resize(item.size)
outputFile()
}
}
}
You can find more informations about how to use this plugin on the wiki page on GitHub.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With