问题描述
我在绘制目录与960x1440大小的图像文件。当应用程序启动时,图像不加载作为背景及在我的logcat见
位图太大,无法上传到纹理(2880x4320,上限= 4096×4096)
为什么说2880x4320如果图像是960x1440?
位图是通过XML加载:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_centerHorizontal =真
机器人:layout_centerVertical =真
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/ launcher_bg
机器人:方向=垂直
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin>
在图像/绘制/
文件夹中没有任何规范被认为是默认,这对于1DP = 1px的就是 MPDI
,然后因为你实际运行的设备是 xxhdpi
的图像得到的在运行时扩大规模。
原始图像可能是960x1440,但是从 MDPI
转化为 xxhdpi
的3倍大小,所以你的960x1440成为(3 * 960)×(3 * 1440)= 2880x4320,这是过大的纹理应用到硬件加速的观点
所以要解决这个问题其实是pretty的简单,你有两个选择:
- 将您的图像
/绘制-nodpi /
这是简单,降低了.apk文件大小,但低端设备可能很难加载这么大的图像。李> - 创建所有的密度缩放图像
MDPI
,华电国际
,xhdpi
,xxhdpi
来避免运行时过度缩放和在旧设备具有更小的图像。
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:
Bitmap too large to be uploaded into a texture (2880x4320, max=4096x4096)
Why it says 2880x4320 if the image is 960x1440 ?
The bitmap is loaded via 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">
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.
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:
- 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.
这篇关于位图太大而不能上传到纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!