android 代碼設置PNG圖片失真問題

今天測試那邊反饋一個問題,我們的遊戲背景的漸變處有明顯的光暈失真:先上圖

用藍線圈出來的地方漸變效果有明顯的失真現象

我的代碼是:

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;或者把它註銷掉,就沒失真了

 

最終效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章