最近在開發android media player的專輯封面圖檔
發現會有圖檔設定大小不符合預期的情形 如下面所述程式碼
mBitmap = getMirrorBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.cluster_media_music_icon_albums_disable) , percent);
埋log發現不符合大小是該行程式碼所引起,因此特別去搜尋 找到網路上有些關文章討論跟我發生一樣狀況的文章
BitmapFactory.decodeResource之坑 :https://www.itread01.com/content/1546283285.htmlBitmap resize api
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // CREATE A MATRIX FOR THE MANIPULATION Matrix matrix = new Matrix(); // RESIZE THE BIT MAP matrix.postScale(scaleWidth, scaleHeight); // "RECREATE" THE NEW BITMAP Bitmap resizedBitmap = Bitmap.createBitmap( bm, 0, 0, width, height, matrix, false); //bm.recycle(); return resizedBitmap;}
沒有留言:
張貼留言