在 xxhdpi 屏幕上使用最低 dpi 可绘制对象
The lowest dpi drawable is being used on a xxhdpi screen
我有两个图像按钮,尺寸为 256x256 dp 和 scaleType="centerCrop"
。
当我在 AS 中看到预览或在我的 nexus 5 上启动应用程序时,图像很模糊。图像的尺寸适合各个文件夹,最大为 xxxhdpi(192x192)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/black">
<ImageButton
android:layout_marginTop="48dp"
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/button_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>
<ImageButton
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/second_button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/second_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>
</LinearLayout>
请先查看此文档
https://developer.android.com/guide/topics/resources/providing-resources.html
然后检查您的项目结构。这部分不太可能你弄错了,首先匹配的是低分辨率资源。
如果我没理解错 - 在你的 XML 布局文件中你设置了一个 ImageButton
和 width=256dp
和 height=256dp
,以及 src=drawable/your_drawable。 xxxhdpi
可绘制对象的宽度和高度为 192px
(我假设 mdpi - 48、hdpi - 72、xhdpi - 96、xxhdpi - 144、xxxhdpi - 192)。
如果是这样,当然图像会模糊,因为您设置了 ImageButtons
width/height in mdpi
(在 xml 中为 256dp),但是您使用了 144x144
(nexus 5 is xxhdpi) Drawable stretched (because of the scaleType=centerCrop
).
如果您不想拉伸图像,这里有一些选项(可能不是全部):
- 您需要将 scaleType 更改为 centerInside(或不使用 scaleType)- 您将获得更小的按钮 - 图像仅在需要时缩小
- 您需要将 ImageButton 变小 - 48x48
- 您需要添加更大的可绘制资源 - 您的 mdpi 可绘制资源应与 xml 中的 ImageButton 一样大(mdpi - 256x 256、hdpi - 384x384、xhdpi - 512x512、xxhdpi - 768x768、xxxhdpi - 1024x1024 )
- 你 post 在你的问题中使用了一些布局代码 (XML) 以及你使用的详细的可绘制资源分辨率,也许我们可以进一步调查你的问题
我有两个图像按钮,尺寸为 256x256 dp 和 scaleType="centerCrop"
。
当我在 AS 中看到预览或在我的 nexus 5 上启动应用程序时,图像很模糊。图像的尺寸适合各个文件夹,最大为 xxxhdpi(192x192)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/black">
<ImageButton
android:layout_marginTop="48dp"
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/button_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>
<ImageButton
android:layout_width="256dp"
android:layout_height="256dp"
android:id="@+id/second_button"
android:layout_gravity="center_horizontal"
android:src="@mipmap/second_off"
android:background="@android:color/black"
android:scaleType="centerInside"
/>
</LinearLayout>
请先查看此文档
https://developer.android.com/guide/topics/resources/providing-resources.html
然后检查您的项目结构。这部分不太可能你弄错了,首先匹配的是低分辨率资源。
如果我没理解错 - 在你的 XML 布局文件中你设置了一个 ImageButton
和 width=256dp
和 height=256dp
,以及 src=drawable/your_drawable。 xxxhdpi
可绘制对象的宽度和高度为 192px
(我假设 mdpi - 48、hdpi - 72、xhdpi - 96、xxhdpi - 144、xxxhdpi - 192)。
如果是这样,当然图像会模糊,因为您设置了 ImageButtons
width/height in mdpi
(在 xml 中为 256dp),但是您使用了 144x144
(nexus 5 is xxhdpi) Drawable stretched (because of the scaleType=centerCrop
).
如果您不想拉伸图像,这里有一些选项(可能不是全部):
- 您需要将 scaleType 更改为 centerInside(或不使用 scaleType)- 您将获得更小的按钮 - 图像仅在需要时缩小
- 您需要将 ImageButton 变小 - 48x48
- 您需要添加更大的可绘制资源 - 您的 mdpi 可绘制资源应与 xml 中的 ImageButton 一样大(mdpi - 256x 256、hdpi - 384x384、xhdpi - 512x512、xxhdpi - 768x768、xxxhdpi - 1024x1024 )
- 你 post 在你的问题中使用了一些布局代码 (XML) 以及你使用的详细的可绘制资源分辨率,也许我们可以进一步调查你的问题