今天测试那边反馈一个问题,我们的游戏背景的渐变处有明显的光晕失真:先上图
用蓝线圈出来的地方渐变效果有明显的失真现象
我的代码是:
gameBg.setBackgroundDrawable(ImageUtil.getResDrawable(R.drawable.join_bj));
在网上找了一下都提到:在Activity的onCreate()方法中直接设置 getWindow().setFormat(PixelFormat.RGBA_8888);
资料链接:http://blog.sina.com.cn/s/blog_7f56ca7b0100uz4g.html
感觉说的挺在理的,但是尝试之后,还是没任何改变。
这是为什么呢,我在xml文件中直接设置和用gameBg.setBackgroundResource(R.drawable.join_bj);设置都不会失真
我再自己检查原来是应为之前的开发为了控制内存溢出,创建了一个工具类来进行缓存
ImageUtil.getResDrawable(R.drawable.join_bj)这个就是调用了缓存,而在我的工具方法里面有一段这样设置的
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
BitmapDrawable drawable = new BitmapDrawable(context.getResources(), BitmapFactory.decodeStream(
context.getResources().openRawResource(drawableId), null, opt));
WeakReference<Drawable> softDrawable = new WeakReference<Drawable>(drawable);
drawable = null;
imageWeakMap.put(String.valueOf(drawableId), softDrawable);
其中有这句话:opt.inPreferredConfig = Bitmap.Config.RGB_565;
这个是当初为了降低图片占用的内存,设的参数,也是应为这个参数降低了图片的质量,使得图片失真了。
我把它改成了opt.inPreferredConfig = Bitmap.Config.ARGB_8888;或者把它注销掉,就没失真了
最终效果: