简体   繁体   中英

How generate images with different resolution for android project?

I have a lot of images with xhdpi resolution. Now I need the same images but in mdpi, ldpi, hdpi resoulution. I think this process could be automated. So, what is the best way to generate images with different resolution?

Try using:

http://android-ui-utils.googlecode.com/hg/asset-studio/dist/index.html

This helps a lot in generating resources. Hope it helps!

The process is already automated. If you provide just one size - hdpi works for me - then the Android system will resize or rescale it as needed for other screen resolutions and sizes.

The drawback of not providing images in different sizes is that the automatic resizing algorithm might not do as good a job as a graphic designer. However, if you are looking for a non Android algorithm to do the resizing, who's to say it will be any better than the built-in algorithm?

I suggest trying just one size on various devices and see how your graphics looks. Some images will scale better than others, so you may only need to provide multiples for a few.

Not too long ago I needed to resize a Image in java and I found a very good method for that, it returns a type of bufferedimage and you will have to use that new instance. You can specicify your width and height. Here is the code:

public BufferedImage resizeImage(final Image image, int width, int height) {
        final BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setComposite(AlphaComposite.Src);
        graphics2D.drawImage(image, 0, 0, width, height, null);
        graphics2D.dispose();

        return bufferedImage;
    }

Just putting in 1 image at a higher resolution (like xhdpi or hdpi) and using that for all scales is not efficient as while android does resize it to fit, it still is loading the image in its original size into memory, thus this would cause some lag or outOfMemory errors if you have lot of them in 1 activity.

I would use the bitmap Image loader that google recommends.

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#decodeSampledBitmapFromResource

This lets you take an image, and it will resize it, while only loading the new image size into memory. This is also the method you would want to do if you want a "lazy load".

The solution I chose to handle all my drawables is the following:

1- Have all drawables in high resolution in a /etc folder in your app project. For the high resolution I chose 10 times the default resolution (mdpi). So for instance the launcher icon drawables (48dip x 48dip which is 48 x 48 pixels in mdpi) would be 480 x 480 pixels. I have all my drawables in high res in the /etc folder.

2- At build time, my ant script would take all drawables from /etc, and resize them to fill all /res/drawables-* folders. From the highres drawable at 100% size, the various resolutions sizes will be: ldpi: 7.5%, mdpi: 10%, hdpi: 15%, xhdpi: 20%, xxhdpi: 30%

I use an external program launched by ant to actually do the resizing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM