android:多個bitmap橫向、縱向拼接成一個

一、橫向(兩個)

/** 
* 橫向拼接 
* <功能詳細描述> 
* @param first 
* @param second 
* @return 
*/ 
private Bitmap add2Bitmap(Bitmap first, Bitmap second) { 
int width = first.getWidth() + second.getWidth(); 
int height = Math.max(first.getHeight(), second.getHeight()); 
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888); 
Canvas canvas = new Canvas(result); 
canvas.drawBitmap(first, 0, 0, null); 
canvas.drawBitmap(second, first.getWidth(), 0, null); 
return result;

}

二、縱向(兩個)

/**
* 縱向拼接
* <功能詳細描述>
* @param first
* @param second
* @return
*/
private Bitmap addBitmap(Bitmap first, Bitmap second) {
int width = Math.max(first.getWidth(),second.getWidth());
int height = first.getHeight() + second.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawBitmap(second, 0, first.getHeight(),null);
return result;
}

三、縱向(多個)

/**
 * 將bitmap集合上下拼接
 *
 * @return
 */
private Bitmap drawMulti(ArrayList<Bitmap> bitmaps) {
    int width = bitmaps.get(0).getWidth();
    int height = bitmaps.get(0).getHeight();
    for (int i = 1;i<bitmaps.size();i++) {
        if (width < bitmaps.get(i).getWidth()) {
            width = bitmaps.get(i).getWidth();
        }
        height = height+bitmaps.get(i).getHeight();
    }
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(bitmaps.get(0), 0, 0, paint);
    int h = 0;
    for (int j = 1;j<bitmaps.size();j++) {
        h = bitmaps.get(j).getHeight()+h;
        canvas.drawBitmap(bitmaps.get(j), 0,h, paint);
    }
    return result;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章