简体   繁体   English

如何使用 drawable 兼容所有屏幕尺寸(idpi、mdpi、hdpi、xhdpi、xxhdpi)

[英]how to use drawable to compatible with all screen sizes (idpi, mdpi, hdpi, xhdpi, xxhdpi)

Hello I am new in android programing so I want to know:您好,我是 android 编程新手,所以我想知道:

How to use drawable to compatible with all screen sizes (idpi, mdpi, hdpi, xhdpi, xxhdpi)如何使用 drawable 兼容所有屏幕尺寸(idpi、mdpi、hdpi、xhdpi、xxhdpi)

Actually I am confused about drawable screen sizes compatibility with all screen sizes实际上我对可绘制屏幕尺寸与所有屏幕尺寸的兼容性感到困惑

so I want to know that:所以我想知道:

If I want my app to compatible with all screen size do I need to put images in every drawable folder如果我希望我的应用程序与所有屏幕尺寸兼容,我是否需要将图像放在每个可绘制文件夹中

for example: background image例如:背景图片

  • • drawable-ldpi ( 240x320 for QVGA Device 2.7 ) put image with this resolution • drawable-ldpi(对于 QVGA 设备 2.7 为 240x320)以该分辨率放置图像
  • • drawable-mdpi ( 320x480 for HVGA Device 3.2 ) put image with this resolution • drawable-mdpi(对于 HVGA 设备 3.2 为 320x480)以该分辨率放置图像
  • • drawable-hdpi ( 480x800 for WVGA Device 4.0 ) put image with this resolution • drawable-hdpi(WVGA 设备 4.0 为 480x800)以该分辨率放置图像
  • • drawable-xhdpi ( 1280x720 for WxVGA Device 4.7) put image with this resolution • drawable-xhdpi(WxVGA 设备 4.7 为 1280x720)以该分辨率放置图像
  • • drawable-xxhdpi ( 1080x1920 for Nexus 5 Device ) put image with this resolution • drawable-xxhdpi(Nexus 5 设备为 1080x1920)以该分辨率放置图像

or I can use a single same image for all screen sizes.或者我可以为所有屏幕尺寸使用一个相同的图像。

You do not need to create an image in every possible drawable folder.您不需要在每个可能的drawable文件夹中创建图像。 It's enough to provide one image only and put it into drawable folder.仅提供一张图像并将其放入drawable文件夹就足够了。 This image will be scaled automatically, depending on your usage.该图像将根据您的使用情况自动缩放。

This will not provide good quality on all screens though and may be more expensive to compute (scale).但这不会在所有屏幕上提供良好的质量,并且计算(规模)可能更昂贵。 It is recommended that you can provide separate images for different screen densities (eg drawable-mdpi , drawable-hdpi , etc.) and the idea is that they should have different resolutions matching the screen densities.建议您可以为不同的屏幕密度(例如drawable-mdpidrawable-hdpi等)提供单独的图像,其想法是它们应该具有与屏幕密度匹配的不同分辨率。

Your images will only look sharp and crisp if you provide drawables for every dpi.如果您为每个 dpi 提供可绘制对象,您的图像只会看起来清晰锐利。 Ldpi is no longer supported so you can ignore ldpi.不再支持 Ldpi,因此您可以忽略 ldpi。

Also, for your launcher icon, provide an xxxhdpi image.此外,对于您的启动器图标,请提供 xxxhdpi 图像。 Tablets running on Kit Kat with full hd screens use these images in the launcher.在 Kit Kat 上运行的具有全高清屏幕的平板电脑在启动器中使用这些图像。

Creating 1 drawable and putting it in the drawable folder is bad practice!创建 1 个 drawable 并将其放在drawable文件夹中是不好的做法! Your images will look blurry.您的图像看起来很模糊。

I have got a very good method for this situation works like a charm.对于这种情况,我有一个很好的方法,就像一种魅力。 You only need to create one hd image for you and than method works-out everything.您只需要为您创建一个高清图像,然后方法就可以解决所有问题。

Here is the method:这是方法:

/**
 * Get DRAWABLE with original size screen resolution independent
 * @param is Input stream of the drawable
 * @param fileName File name of the drawable
 * @param density Density value of the drawable
 * @param context Current application context
 * @return Drawable rearranged with its original values for all
 * different types of resolutions.
 */
public static Drawable getDrawable(InputStream is, String fileName, int density, Context context) {
    Options opts = new BitmapFactory.Options();
    opts.inDensity = density;
    opts.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
    return Drawable.createFromResourceStream(context.getResources(), null, is, fileName, opts);
}

Here, you must prepare the inputstrem for your image file and set a density that is good for your screen at any usage area of yours.在这里,您必须为您的图像文件准备输入流,并在您的任何使用区域设置适合您的屏幕的密度。 The smaller density is the lower quality, solve out by changing the value.密度越小质量越低,通过改变值解决。 Here are some examples on using the method:以下是使用该方法的一些示例:

1) Open an asset from the assets folder: 1) 从资产文件夹中打开资产:

getDrawable(assetManager.open("image.png"), "any_title", 250, context)

2) Open a drawable from the drawables folder: Here first, you must provide your inputstream with this method: a) method: 2) 从 drawables 文件夹中打开一个 drawable:首先,您必须为您的输入流提供此方法:a) 方法:

/**
 * Get InputStream from a drawable
 * @param context Current application context
 * @param drawableId Id of the file inside drawable folder
 * @return InputStream of the given drawable
 */
public static ByteArrayInputStream getDrawableAsInputStream(Context context, int drawableId) {
    Bitmap bitmap = ((BitmapDrawable)context.getResources().getDrawable(drawableId)).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    return new ByteArrayInputStream(imageInByte);
}

and the usage: b) usage:和用法:b) 用法:

getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)

I hope it is useful.我希望它有用。

you have to create an image in several sizes.您必须创建多种尺寸的图像。 for example: Let's say that you have ImageView with 48dp x 48dp.例如:假设您有 48dp x 48dp 的 ImageView。 Base on this size you have to create image with sizes:基于此大小,您必须创建具有以下大小的图像:

  1. 48x48px(100% - mdpi) 48x48px(100% - mdpi)
  2. 64x64px(150% - hdpi) 64x64px(150% - hdpi)
  3. 96x96px(200% - xhdpi) 96x96px(200% - xhdpi)

    and so on, if you have to support more.等等,如果你必须支持更多。

NOTE: there is no need to create ldpi (75%) resolution image, because your hdpi will simply scale down twice.注意:无需创建 ldpi (75%) 分辨率图像,因为您的 hdpi 只会缩小两次。

If you use one size for several dpi, then there can be poor quality on some resolutions.如果您对多个 dpi 使用一种尺寸,则某些分辨率的质量可能会很差。

You can also use the Android Asset Studio, where you can upload an image and it will generate all of the types of resolution images you need.您还可以使用 Android Asset Studio,您可以在其中上传图像,它会生成您需要的所有类型的分辨率图像。 It can even be used to create animations and icons for your application.它甚至可以用于为您的应用程序创建动画和图标。 Here is the link: https://romannurik.github.io/AndroidAssetStudio/index.html这是链接: https : //romannurik.github.io/AndroidAssetStudio/index.html

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

相关问题 可绘制尺寸:LDPI,MDPI,HDPI,XHDPI,XXHDPI,XXXHDPI? - Sizes for drawable: LDPI, MDPI, HDPI, XHDPI, XXHDPI, XXXHDPI? 我是否需要使用所有drawable,drawable-mdpi,drawable-hdpi,drawable-xhdpi,drawable-xxhdpi和drawable-xxxhdpi? - Do I need to use all of drawable, drawable-mdpi, drawable-hdpi, drawable-xhdpi, drawable-xxhdpi & drawable-xxxhdpi? android mdpi,hdpi,xhdpi,xxhdpi之间的关系如何计算 - android relationship between mdpi, hdpi, xhdpi, xxhdpi how to calculate 其中drawable-hdpi,drawable-mdpi,drawable-xhdpi和drawable-xxhdpi。 走? - where drawable-hdpi ,drawable-mdpi ,drawable-xhdpi and drawable-xxhdpi. go? mdpi、hdpi、xhdpi 和 xxhdpi 的图像分辨率 - Image resolution for mdpi, hdpi, xhdpi and xxhdpi 适用于MDPI,HDPI,XHDPI和XXHDPI的不同APK - Different APK for MDPI, HDPI, XHDPI and XXHDPI 如何使用不同的密度(mdpi、hdpi、xhdpi、...) - How to use the different densities (mdpi, hdpi, xhdpi, …) Android Photoshop操作可导出到mdpi hdpi xhdpi xxhdpi和xxxhdpi - Android Photoshop actions to export to mdpi hdpi xhdpi xxhdpi and xxxhdpi 在ldpi,mdpi,hdpi,xhdpi,xxhdpi中自动将sp相互转换 - Convert sp automatically to each other in ldpi, mdpi, hdpi, xhdpi, xxhdpi 适用于mdpi,hdpi,xhdpi,xxhdpi的Android图像尺寸(仅纵向) - Android image size (portrait only) for mdpi, hdpi, xhdpi, xxhdpi
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM