位图太大无法上传到纹理中 [英] Bitmap too large to be uploaded into a texture
问题描述
我在 drawable 目录中有一个 960x1440 大小的图像文件.当应用程序启动时,图像不会作为背景加载,并且在 logcat 中我看到:
I've in drawable directory an image file with the 960x1440 size. When the app start, the image is not loaded as background and in the logcat I see:
位图太大而无法上传到纹理中 (2880x4320, max=4096x4096)
如果图片是 960x1440,为什么会显示 2880x4320?
Why it says 2880x4320 if the image is 960x1440 ?
位图通过xml加载:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/launcher_bg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
推荐答案
/drawable/
文件夹中没有任何指定的图像被认为是默认",即 1dp = 1px那是 mpdi
,然后因为你实际运行的设备是 xxhdpi
图像在运行时被放大.
an image in the /drawable/
folder without any specification is considered to be the "default", that is for 1dp = 1px that is mpdi
, then because the device you're actually running is xxhdpi
that image get's scaled up during runtime.
原始图像可能是 960x1440,但是从 mdpi
转换为 xxhdpi
的大小是 3 倍,所以你的 960x1440 变成 (3*960)x(3*1440) = 2880x4320,纹理太大,无法应用于硬件加速视图.
The original image might be 960x1440, but the conversion from mdpi
to xxhdpi
is 3 times the size, so your 960x1440 becomes (3*960)x(3*1440) = 2880x4320, which is too large of a texture to apply to the hardware accelerated views.
所以要解决这个问题其实很简单,你有两个选择:
so to fix that is actually pretty simple, you have two choices:
- 将您的图像移动到
/drawable-nodpi/
这很简单,可以减小 .apk 大小,但低端设备可能难以加载如此大的图像. - 在所有密度上创建缩放图像
mdpi
、hdpi
、xhdpi
、xxhdpi
以避免运行时过度缩放并在旧设备上使用较小的图像.
- move your image to
/drawable-nodpi/
that's simple, reduces the .apk size, but lower end devices might struggle to load such a big image. - create scaled images on all densities
mdpi
,hdpi
,xhdpi
,xxhdpi
to avoid runtime over-scaling and to have smaller images on older devices.
这篇关于位图太大无法上传到纹理中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!