[英]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例如:背景图片
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-mdpi
、 drawable-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:基于此大小,您必须创建具有以下大小的图像:
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.