Android Bitmap儲存時背景變為黑色的問題

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

      之前寫的一個Android程式,一直有個bug懸而未決:背景色原來為白色,可儲存圖片時卻變成黑色。昨天又拿出來看了看,突然想到建立Bitmap對象時,預設變數應該和Java中其他建立變數或對象的情況類似,預設值為0。因此猜想建立一個Bitmap時,每個像素的值都是0,即黑色。於是建立一個每個像素點都是255(白色)的Bitmap就行了。

    部分代碼如下。

    得到位元影像的方法:

 1 /** 2      * 得到相應背景色的位元影像 3      * @param width 位元影像的寬度 4      * @param height 位元影像的高度 5      * @param color 位元影像的背景色 6      * @return 該顏色的位元影像 7      */ 8     public Bitmap getBitmapByColor(int width,int height,int color){ 9         Bitmap newBitmap;10         int[] colors=new int[width*height];//建立像素點數組,數組元素個數是位元影像的寬乘以高11         for (int i=0;i<colors.length;i++){12             colors[i]=color;//將顏色賦值給每一個像素點13         }14         newBitmap= createBitmap(colors,width,height,Bitmap.Config.ARGB_8888);15         return newBitmap;16 }
View Code

    構造方法:

1 public DrawView(Context context, AttributeSet attributeSet) {2         super(context, attributeSet);3         int width = context.getResources().getDisplayMetrics().widthPixels;//得到螢幕的寬度4         int height = context.getResources().getDisplayMetrics().heightPixels;//得到螢幕的高度5         Bitmap bitmap=getBitmapByColor(width,height,Color.WHITE);6         Canvas canvas = new Canvas();7         Canvas.setBitmap(bitmap);8         }
View Code

    儲存圖片的方法:

 1 /** 2      * 將圖片儲存在記憶卡的pictures檔案夾內 3      * @param fileName 檔案名稱 4      * @throws IOException 5      */ 6     public void savePic(String fileName) throws IOException { 7         File file = new File("/sdcard/pictures/" + fileName + ".png"); 8         file.createNewFile(); 9         FileOutputStream fileOS = new FileOutputStream(file);10         cacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOS);//注意是PNG格式的。若設定為JPG格式,背景色會變黑11         fileOS.flush();12         fileOS.close();13     }
View Code

     改成這樣,運行後發現報錯!進行斷點調試,發現” canvas.setBitmap(bitmap);”出錯了。在網上查了一下,原來Canvas對象在執行setBitmap方法時,首先判斷這個位元影像是不是可變的,如果是不可變的,那麼便不能執行setBitmap()方法。bitmap.isMutable()返回一個布爾值。如果bitmap是可變的,則返回true;反之返回false。將這條語句嵌入到代碼中,logcat上顯示bitmap確實是不可變的。因此得想辦法將Bitmap對象變為可變才行。在網上查到,只有一種方法可行,那就是調用bitmap的copy()方法,拷貝一份給另一個Bitmap對象,copy()有一個參數,可以設定拷貝的一份是不是可變的。不過這樣原來的Bitmap對象就沒什麼用了。

     因此其他方法不變,將構造方法改為:

1 public DrawView(Context context, AttributeSet attributeSet) {2         super(context, attributeSet);3         int width = context.getResources().getDisplayMetrics().widthPixels;//得到螢幕的寬度4         int height = context.getResources().getDisplayMetrics().heightPixels;//得到螢幕的高度5         Bitmap tempBitmap=getBitmapByColor(width,height,Color.WHITE);6         Canvas canvas = new Canvas();7         Bitmap bitmap=tempBitmap.copy(tempBitmap.getConfig(),true);//true表示該bitmap對象是可變的;false則反之8         canvas.setBitmap(bitmap);9 }
View Code

    這樣便運行成功了。

     其實還有一種方法,就是先執行createBitmap()方法,建立一個Bitmap對象。然後將這個Bitmap對象的像素點全部設定為想要的顏色。經測試發現,這樣不會導致bitmap變為不可變的。

     部分代碼如下。     

     設定位元影像背景色的方法:

 1 /** 2      * 設定位元影像的背景色 3      * @param bitmap 需要設定的位元影像 4      * @param color 背景色 5      */ 6 public void setBitmapBGColor(Bitmap bitmap,int color){ 7     for(int i=0;i<bitmap.getWidth();i++){ 8         for(int j=0;j<bitmap.getHeight();j++){ 9             bitmap.setPixel(i,j,color);//將bitmap的每個像素點都設定成相應的顏色10         }11     }12 }
View Code

    構造方法:

1 public DrawView(Context context, AttributeSet attributeSet) {2         super(context, attributeSet);3         width = context.getResources().getDisplayMetrics().widthPixels;//得到螢幕的寬度4         height = context.getResources().getDisplayMetrics().heightPixels;//得到螢幕的高度5         Bitmap bitmap=createBitmap(width,height,Bitmap.Config.ARGB_8888);6         setBitmapBGColor(bitmap,Color.WHITE);7         Canvas canvas = new Canvas();8         canvas.setBitmap(bitmap);9 }
View Code

    儲存圖片的方法不變。以上所有的構造方法只是給出了部分代碼。

 

Android Bitmap儲存時背景變為黑色的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.