Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool to batch convert android resource bitmaps to different densities [closed]

I need to post-release support different display densities on Android

During development drawable-hdpi has grown to 160 png (like 20 of them 9 patch) assets

I now need to convert all those bitmaps to mdpi, ldpi (layouts and drawables XML are already fine, to avoid raising OOM on LayoutInflater

Is there any tool suitable to convert all those bitmaps in batch ?

like image 565
Marek Sebera Avatar asked Sep 28 '12 13:09

Marek Sebera


People also ask

How do you handle bitmaps in android as it takes too much memory?

If you're displaying large amounts of bitmap data in your app, you're likely to run into OutOfMemoryError errors. The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used.

What are Drawables in android?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

What is stroke in android XML?

Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.

How do I make a drawable Xxhdpi folder?

Simply go to project Explorer and change your View from Android to project from drop Down and you are good to go. There you can simply create folder like we do in Eclipse. And in android project view it is hidden but when you switch to project. You can create folder like drawable-hdpi,drawable-xhdpi .


2 Answers

Here is a simple script to create lower-resolution Android drawables from higher-resolution ones.

For example, given a batch of -xhdpi images, it can generate -hdpi and -mdpi images.

With this script, it’s possible to export only highest-resolution artwork from authoring tools and then create the lower-resolution versions with a few batch commands.

Script usage:

drawable_convert.py -d res/drawable-mdpi -d res/drawable-hdpi res/drawable-xhdpi-v14/*.png

This will take every png file from xhdpi and place lower-resolution versions into mdpi and hdpi folders.

http://kmansoft.com/2012/05/23/scale-android-drawables-with-a-script/
original script, https://gist.github.com/2771791


Script itself, to avoid dependency on github gist/original blogpost.
name drawable_convert.py

#!/usr/bin/python

import sys
import argparse
import os
import re

'''
A simple script to create lower-resolution Android drawables from higher-resolution ones.

For example, given a batch of -xhdpi images, you can generate -hdpi and -mdpi images.

This makes it possible to only export highest-resolution artwork from image authoring tools, and
automate the rest.

Usage:

   drawable_convert.py -d res/drawable-mdpi -d res/drawable-hdpi res/drawable-xhdpi-v14/select*.png

   ... will take select*.png from xhdpi and place versions into mdpi and hdpi folders.

   Correct resize ratios are computed based on resource directory names.

   Actual scaling is done by ImageMagick's convert command.
'''

class Converter:
    def __init__(self, dstList):
        print u'Dst list: {0}'.format(dstList)
        self.mDstList = dstList

    def convert(self, src):
        for dstpath in self.mDstList:
            (srcpath, srcname) = os.path.split(src)
            dst = os.path.join(dstpath, srcname)
            self.convertOne(src, dst)

    def convertOne(self, src, dst):
        print u'\n*****\n{0} to {1}\n*****\n'.format(src, dst)
        '''
        Determine relative density
        '''
        srcDpi = self.getDpi(src)
        dstDpi = self.getDpi(dst)

        if srcDpi < dstDpi:
            print u'NOT converting from {0}dpi to {1}dpi'.format(srcDpi, dstDpi)
        else:
            factor = dstDpi*100/srcDpi
            print u'Converting from {0}dpi to {1}dpi, {2}%'.format(srcDpi, dstDpi, factor)
            cmd = u'convert -verbose "{0}" -resize "{2}%x{2}%" "{1}"'.format(src, dst, factor)
            os.system(cmd)

    def getDpi(self, f):
        p = os.path.dirname(f)
        if re.match('.*drawable.*\\-mdpi.*', p):
            return 160
        elif re.match('.*drawable.*\\-hdpi.*', p):
            return 240
        elif re.match('.*drawable.*\\-xhdpi.*', p):
            return 320
        else:
            raise ValueError(u'Cannot determine densitiy for {0}'.format(p))

if __name__ == "__main__":
    '''
    Parse command line arguments
    '''
    parser = argparse.ArgumentParser(description='Converts drawable resources in Android applications')
    parser.add_argument('-d', dest='DST', action='append', required=True, help='destination directory')
    parser.add_argument('src', nargs='+', help='files to convert (one or more)')
    args = parser.parse_args()

    cv = Converter(args.DST)
    for src in args.src:
        cv.convert(src)


'''


if [ $# -lt 1 ] ; then
    echo "Usage: $0 file_list"
    exit 1
fi

for f in $*
do
    echo "File: ${f}"
    convert -verbose "${f}" -resize "75%x75%" "../drawable-hdpi/${f}"
    convert -verbose "${f}" -resize "50%x50%" "../drawable-mdpi/${f}"
done

'''
like image 66
Sababado Avatar answered Nov 09 '22 03:11

Sababado


Final-Android-Resizer did the job for me and was simple to use but had all the options I needed: https://github.com/asystat/Final-Android-Resizer

like image 42
David Boyd Avatar answered Nov 09 '22 03:11

David Boyd