2013-01-10 19 views
1

我想將兩個位圖並排合併到一個位圖中。以下代碼是合併子底部。如何並行合併到一個位圖中?如何並列合併位圖

public Bitmap mergeBitmap(Bitmap fr, Bitmap sc) 
{ 

    Bitmap comboBitmap; 

    int width, height; 

    width = fr.getWidth() + sc.getWidth(); 
    height = fr.getHeight(); 

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

    Canvas comboImage = new Canvas(comboBitmap); 


    comboImage.drawBitmap(fr, 0f, 0f, null); 
    comboImage.drawBitmap(sc, 0f , fr.getHeight(), null); 
    return comboBitmap; 

} 
+0

改用x -coordinate; ''comboImage.drawBitmap(sc,fr.getWidth(),0f,null)''。 – harism

+0

不幸的是,我在屏幕上看到「fr」。 sc從screeen溢出 – NabukkadNezzar

+0

我覺得,問題是canvas功能。代碼的第一個狀態是 width = fr.getWidth();代碼的第一個狀態是 width = fr.getWidth(); height = fr.getHeight()+ sc.getWidth(); 我改變了之後; width = fr.getWidth()+ sc.getWidth(); height = fr.getHeight(); – NabukkadNezzar

回答

1
public Bitmap mergeBitmap(Bitmap fr, Bitmap sc) 
    { 

     Bitmap comboBitmap; 

     int width, height; 

     width = fr.getWidth() + sc.getWidth(); 
     height = fr.getHeight(); 

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

     Canvas comboImage = new Canvas(comboBitmap); 


     comboImage.drawBitmap(fr, 0f, 0f, null); 
     comboImage.drawBitmap(sc, fr.getWidth(), 0f , null); 
     return comboBitmap; 

    } 
0

article經過組合2個圖像的一個下方的其他的處理(僅適用於PNG或JPG)。它將涉及傳遞2個Bitmaps,然後將使用Canvas類進行組合。你可以做一些小小的改變,讓你的兩幅圖像並排:

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.getHeight() > s.getHeight()) { 
     width = c.getWidth() + s.getWidth(; 
     height = c.getHeight()); 
    } else { 
     width = c.getWidth() + s.getWidth(); 
     height = s.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; 
    }