简体   繁体   English

如何为Android项目生成具有不同分辨率的图像?

[英]How generate images with different resolution for android project?

I have a lot of images with xhdpi resolution. 我有很多xhdpi分辨率的图像。 Now I need the same images but in mdpi, ldpi, hdpi resoulution. 现在我需要相同的图像,但是分辨率为mdpi,ldpi,hdpi。 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 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. 如果您仅提供一种尺寸-hdpi适用于我-那么Android系统将根据其他屏幕分辨率和尺寸的需要调整其大小或重新缩放。

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? 但是,如果您正在寻找一种非Android算法来进行大小调整,谁又说它会比内置算法更好呢?

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. 不久之前,我需要在Java中调整Image的大小,为此我找到了一个很好的方法,它返回了bufferedimage的类型,您将不得不使用该新实例。 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. 仅以较高的分辨率(例如xhdpi或hdpi)放入1张图像并将其用于所有比例是没有效率的,因为android确实对其进行了调整以适应其大小,但它仍将原始尺寸的图像加载到内存中,因此这将导致如果您在1个活动中有很多延迟或内存不足错误。

I would use the bitmap Image loader that google recommends. 我会使用谷歌推荐的位图图像加载器。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#decodeSampledBitmapFromResource 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. 1-在您的应用程序项目的/ etc文件夹中具有高分辨率的所有图形。 For the high resolution I chose 10 times the default resolution (mdpi). 对于高分辨率,我选择了默认分辨率(mdpi)的10倍。 So for instance the launcher icon drawables (48dip x 48dip which is 48 x 48 pixels in mdpi) would be 480 x 480 pixels. 因此,例如启动器图标可绘制对象(48dip x 48dip,以mdpi为48 x 48像素)将为480 x 480像素。 I have all my drawables in high res in the /etc folder. 我在/ etc文件夹中拥有所有高分辨率的可绘制对象。

2- At build time, my ant script would take all drawables from /etc, and resize them to fill all /res/drawables-* folders. 2-在构建时,我的ant脚本将从/ etc中提取所有可绘制对象,并调整它们的大小以填充所有/ res / drawables- *文件夹。 From the highres drawable at 100% size, the various resolutions sizes will be: ldpi: 7.5%, mdpi: 10%, hdpi: 15%, xhdpi: 20%, xxhdpi: 30% 从100%尺寸的可绘制高分辨率图像来看,各种分辨率的尺寸为:ldpi:7.5%,mdpi:10%,hdpi:15%,xhdpi:20%,xxhdpi:30%

I use an external program launched by ant to actually do the resizing. 我使用由ant启动的外部程序来实际进行大小调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为不同分辨率的图像制作相同的比例尺? - How to make an equal Scale for images of different resolution? 如何在 Android Studio 中显示高分辨率的图像? - How to show images with a high resolution in Android Studio? 如何将图像导入Android项目? - How to import images into an Android project? 如何使用 JDK 11 在 Android 项目中生成 javadoc - How to generate javadoc in an Android project with JDK 11 如何使用Gradle在Android项目中生成Jacoco报告 - How to generate jacoco report in android project with gradle Java裁剪不同分辨率图像的特定部分 - Java crop specific part of different resolution images 如何为java项目生成jar文件并在android项目中使用jar - How to generate a jar file for a java project and use the jar in an android project 如何根据java或python中的一张图像生成不同的彩色图像? - how to generate different color images according to one image in java or python? 如何配置 Maven 项目为接口的每个实现生成不同的 jar - How to configure a Maven project to generate a different jar for each implementation of an interface 如何在Android中使用不同的参数在同一活动中裁剪两个不同的图像? - How to crop two different images in the same activity with different parameters in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM