首页 文章

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

提问于
浏览
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设备4.7为1280x720)放置此分辨率的图像

  • •drawable-xxhdpi(对于Nexus 5设备为1080x1920)以此分辨率放置图像

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

5 回答

  • 1

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

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

  • 2

    如果您为每个dpi提供drawable,您的图像将只显得清晰锐利 . 不再支持Ldpi,因此您可以忽略ldpi .

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

    创建1个drawable并将其放在 drawable 文件夹中是不好的做法!您的图像看起来很模糊 .

  • 4

    我有一个非常好的方法,这种情况就像一个魅力 . 你只需要为你创建一个高清图像,而不是方法可以解决所有问题 .

    这是方法:

    /**
     * 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)从资产文件夹中打开资产:

    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)
    

    我希望它有用 .

  • 0

    你必须创建几种尺寸的图像 . 例如:假设您有48dp x 48dp的ImageView . 基于此尺寸,您必须创建尺寸为的图像:

    • 48x48px(100% - mdpi)

    • 64x64px(150% - hdpi)

    • 96x96px(200% - xhdpi)

    等等,如果你需要支持更多 .

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

    如果你使用一种尺寸的dpi,那么某些分辨率的质量可能会很差 .

  • 0

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

相关问题