1

您好,我是 android 编程新手,所以我想知道:

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

实际上,我对可绘制的屏幕尺寸与所有屏幕尺寸的兼容性感到困惑

所以我想知道:

如果我希望我的应用程序与所有屏幕尺寸兼容,我是否需要将图像放在每个可绘制文件夹中

例如:背景图片

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

或者我可以为所有屏幕尺寸使用一个相同的图像。

4

5 回答 5

6

您不需要在每个可能的drawable文件夹中创建图像。只提供一张图片并将其放入drawable文件夹就足够了。此图像将根据您的使用情况自动缩放。

但是,这不会在所有屏幕上提供良好的质量,并且计算(缩放)可能更昂贵。建议您可以为不同的屏幕密度(例如drawable-mdpidrawable-hdpi等)提供单独的图像,并且想法是它们应该具有与屏幕密度匹配的不同分辨率。

于 2014-03-20T10:31:27.663 回答
2

如果您为每个 dpi 提供可绘制对象,您的图像只会看起来清晰锐利。不再支持 Ldpi,因此您可以忽略 ldpi。

此外,对于您的启动器图标,请提供 xxxhdpi 图像。在全高清屏幕的 Kit Kat 上运行的平板电脑在启动器中使用这些图像。

创建 1 个可绘制对象并将其放入drawable文件夹中是不好的做法!您的图像看起来会很模糊。

于 2014-03-20T10:51:19.100 回答
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);
}

在这里,您必须为您的图像文件准备输入流,并在您的任何使用区域设置适合您的屏幕的密度。密度越小质量越低,通过改变数值来解决。以下是使用该方法的一些示例:

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)

我希望它是有用的。

于 2015-03-09T08:39:35.190 回答
0

您必须创建多种尺寸的图像。例如:假设您有 48dp x 48dp 的 ImageView。基于此尺寸,您必须创建具有以下尺寸的图像:

  1. 48x48 像素(100% - mdpi)
  2. 64x64 像素(150% - hdpi)
  3. 96x96 像素(200% - xhdpi)

    等等,如果你必须支持更多。

注意:无需创建 ldpi (75%) 分辨率图像,因为您的 hdpi 只会缩小两倍。

如果您对多个 dpi 使用一种尺寸,则某些分辨率的质量可能会很差。

于 2014-03-20T10:38:01.913 回答
0

您还可以使用 Android Asset Studio,您可以在其中上传图像,它会生成您需要的所有类型的分辨率图像。它甚至可以用于为您的应用程序创建动画和图标。这是链接: https ://romannurik.github.io/AndroidAssetStudio/index.html

于 2017-05-30T23:17:47.760 回答