您好我是android编程的新手,所以我想知道:
如何使用drawable兼容所有屏幕尺寸(idpi,mdpi,hdpi,xhdpi,xxhdpi)
实际上我对可绘制的屏幕尺寸与所有屏幕尺寸的兼容性感到困惑
所以我想知道:
如果我希望我的应用与所有屏幕尺寸兼容,我需要将图像放在每个可绘制文件夹中
例如:背景图片
或者我可以为所有屏幕尺寸使用单个相同的图像。
答案 0 :(得分:4)
您无需在每个可能的drawable
文件夹中创建图像。仅提供一个图像并将其放入drawable
文件夹就足够了。此图片将根据您的使用情况自动缩放。
虽然这并不能在所有屏幕上提供良好的质量,但计算(比例)可能更昂贵。 建议您可以为不同的屏幕密度(例如drawable-mdpi
,drawable-hdpi
等)提供单独的图像,并且想法是它们应该具有与屏幕匹配的不同分辨率密度
答案 1 :(得分:2)
如果您为每个dpi提供drawable,您的图像将只会看起来清晰锐利。不再支持Ldpi,因此您可以忽略ldpi。
此外,对于您的启动器图标,请提供xxxhdpi图像。在具有全高清屏幕的Kit Kat上运行的平板电脑在启动器中使用这些图像。
创建1个drawable并将其放入drawable
文件夹是不好的做法!你的图像看起来很模糊。
答案 2 :(得分:1)
我有一个非常好的方法,这种情况就像一个魅力。你只需要为你创建一个高清图像,而不是方法可以解决所有问题。
以下是方法:
/**
* 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);
}
在此,您必须为图像文件准备inputstrem,并在您的任何使用区域设置适合您屏幕的密度。密度越小,质量越低,通过改变值来解决。以下是使用该方法的一些示例:
1)从assets文件夹中打开资产:
getDrawable(assetManager.open("image.png"), "any_title", 250, context)
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);
}
和用法: b)用法:
getDrawable(getDrawableAsInputStream(getBaseContext(), R.drawable.a_drawable), "any_title", 250, context)
我希望它很有用。
答案 3 :(得分:0)
您必须创建多种尺寸的图像。例如: 让我们说你有48dp x 48dp的ImageView。 基于此尺寸,您必须创建尺寸为的图像:
96x96px(200% - xhdpi)
等等,如果你需要支持更多。
注意: 没有必要创建ldpi(75%)分辨率图像,因为你的hdpi只会缩小两次。
如果您使用一种尺寸的dpi,那么某些分辨率的质量可能会很差。
答案 4 :(得分:0)
您还可以使用Android Asset Studio上传图像,它将生成您需要的所有类型的分辨率图像。它甚至可以用于为您的应用程序创建动画和图标。链接在这里: https://romannurik.github.io/AndroidAssetStudio/index.html