最近在做PhotoView图片的效果定制时,在加载展示图片情境下,统一把图片按照屏幕宽度作为固定值,计算宽高的缩放比然后对Bitmap进行伸缩,这样可以避免一般情况下的大图加载产生——OOM和trying to draw too large(xxxbytes) bitmap的问题。
当即便这样,也还是会有加载的图片尺寸超过限制的时候,就经常会看到这个warning,图片显示不出来:
Bitmap too large to be uploaded into a texture (1080x9431, max=8192x8192)
Bitmap too large to be uploaded into a texture (1080x9431, max=8192x8192)
因为指定的比例的按照屏幕宽度进行宽高比缩放,宽度到了合适的范围,但是当图片太长的时候,还是会触发上面的问题,无法显示图片。
方案一:关闭硬件加速
上面的警告是由于OpenGL硬件加速造成的,可以在Activity、Application、Window、甚至View中把硬件加速关闭即刻。关闭的方式如下:
1、硬件加速的级别
Application
<application
android:hardwareAccelerated="false"
...>
</application>
Activity
<application
android:hardwareAccelerated="true">
<activity ... />
<activity android:hardwareAccelerated="false" />
</application>
Window
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
View
myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Note: 你可以关闭View级别的硬件加速,但是你不能在View级别开启硬件加速,因为它还依赖其他的设置
2、两种获取是否支持硬件加速的方式
//returns true if the View