2014-10-20 74 views
0

我有一個項目的名稱旁邊顯示位圖的項目列表。這個位圖是從2個圖像創建的,我有一個背景圖像和一個較小的前景圖像添加到背景的頂部。Android從兩個位圖創建位圖底部位圖不可見

我看到背景圖片似乎沒有出現在我列表中的某些行上。在沒有背景的情況下哪些行具有組合位圖並不一致。它並不總是相同的行,其中組合位圖沒有背景,並且它不總是第一行或不總是最後一行位圖沒有背景。有時候,整個列表中的每一行都有正確的圖像。

下面的圖片是一個模型,顯示我的問題。

Issue mockup image

我的用於創建組合的位圖的代碼如下。

Bitmap combinedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
Canvas combinedCanvas = new Canvas(combinedBitmap); 

// Add the first bitmap to the canvas (this is my background and this is what appears to be 
// missing on some rows in my list on some occasions) 
combinedCanvas.drawBitmap(backgroundBitmap, 0, 0, null); 

// my second smaller image, on top of the first image but 1 pixel in 
// from the left and 20 pixels down from the top 
combinedCanvas.drawBitmap(foregroundBitmap, 1, 20, null); 

return combinedBitmap; 

注:我BackgroundBitmap指令從繪製對象使用以下代碼

Bitmap backgroundBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), 
               drawable.getMinimumHeight(), 
               Bitmap.Config.ARGB_8888); 
backgroundBitmap.setDensity(resources.getDisplayMetrics().densityDpi); 
Canvas canvas = new Canvas(backgroundBitmap); 
drawable.draw(canvas); 

的任何建議產生我有什麼錯,甚至在哪裏看,試圖解決此將不勝感激。

編輯:我已經測試添加顏色我combinedCanvas的背景下,試圖看到的圖像生成是通過添加以下代碼

// TEMP: fill the canvas in red for now so I can see which combinedBitmaps are missing 
// the background image 
combinedCanvas.drawColor(Color.RED); 

現在不具備後臺行腳麻被塗成紅色。這表示上面創建組合畫布的代碼不知何故添加了backgroundBitmap。我已經檢查過,並且我的背景圖像對於列表中的每一行都不爲空。

回答

1

此方法適用於我。它在C#(Xamarin)中,你必須把它翻譯成Java,恐怕。

public static Bitmap CombineImages(Bitmap background, Bitmap foreground) 
    { 

     int width = background.Width, height = background.Height; 
     Bitmap cs = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); 
     Canvas comboImage = new Canvas(cs); 
     background = Bitmap.CreateScaledBitmap(background, width, height, true); 
     comboImage.DrawBitmap(background, 0, 0, null); 

     int top = (int)(0.05 * height); 
     int left = (int)(width - (foreground.Width + (width * 0.05))); 

     comboImage.DrawBitmap(foreground, left, top, null); 

     return cs; 
    } 

左側和頂部是根據我的要求硬編碼的,最好將它們作爲參數傳遞。

+0

感謝您的回覆,這與我的代碼不太相似,所以我無法看到哪個會導致我的問題。我的代碼似乎間歇性地工作。我想知道是否它的事實,我有這些生成的縮略圖在列表中。 – se22as 2014-10-28 16:31:35