2014-01-28 41 views
0

我正在處理一組分層圖像(認爲堆疊),我需要將它們合併爲一個元素。在Android中,爲什麼我的組合位圖是空白的?

我立足我解掉Combine multiple bitmap into one

//send a map to the method that has my stored image locations in order 
private Bitmap combineImageIntoOne(NavigableMap<Integer, String> layerImages) { 
     //size of my bitmaps 
     int w = 400, h = 400; 
     //bitmap placeholder 
     Bitmap productIndex = null; 

     //flattened layers 
     Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
     //canvas to write layers to 
     Canvas canvas = new Canvas(temp); 
     int top = 0; 
     for (Map.Entry<Integer, String> e : layerImages.entrySet()) { 

      //create the layer bitmap 
      productIndex = decodeSampledBitmapFromResource(getResources(), e.getValue(), 400, 400); 

      //add layer to canvas 
      canvas.drawBitmap(productIndex, 0f, top, null); 
     } 

     //convert temp to a BitmapDrawable 
     Drawable d = new BitmapDrawable(getResources(),temp); 

     //set my image view to have the flattened image 
     carBase.setImageDrawable(d); 

     return temp; 
    } 

decodeSampledBitmapFromResource來自有關加載大位圖在Android文檔:Loading Large Bitmaps Efficiently您可以查看有關該文檔的代碼,看看我「米做什麼,我沒編輯Android代碼太多了

我一直在使用Android代碼,很好地將圖層添加到FrameLayout中,但是當圖層數量開始變得很大時,結果內存不足了。用於節省內存空間。

任何想法爲什麼最終的位圖沒有任何內容?

回答

0

Reference LINK <-------------------------

public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
     width = c.getWidth() + s.getWidth(); 
     height = c.getHeight(); 
    } else { 
     width = s.getWidth() + s.getWidth(); 
     height = c.getHeight(); 
    } 

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 0f, null); 

    // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
    /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 

    OutputStream os = null; 
    try { 
     os = new FileOutputStream(loc + tmpImg); 
     cs.compress(CompressFormat.PNG, 100, os); 
    } catch(IOException e) { 
     Log.e("combineImages", "problem combining images", e); 
    }*/ 

    return cs; 
    } 
+0

感謝這個,我會進行審覈和鏈接,看看我是否可以把它爲我工作。我注意到它說「兩個圖像」我有一個數組,可能有......可能...... 10個或更多。我可能能夠修改這個方法的循環,並得到我想要的結果。謝謝。 – dcp3450

相關問題