因为公司项目需要,有一个群组聊天,头像显示是三个或者四个用户的头像的拼接,可能是品字型也可能是田字型。
自己想了一下,简单实现了一下,求大神们指教。谢谢
代码:
/** 横向连接两张图片 **/
public static Bitmap addHorizontalBitmap(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);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(20);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
canvas.drawLine(first.getWidth(), 0, first.getWidth(),
first.getHeight(), paint);
canvas.drawBitmap(second, first.getWidth(), 0, null);
return result;
}
/** 纵向连接两张图片 **/
public static Bitmap addVerticalBitmap(Bitmap first, Bitmap second) {
int height = first.getHeight() + second.getHeight();
int width = Math.max(first.getWidth(), second.getWidth());
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, 0, 0, null);
paint.setStrokeWidth(20);
canvas.drawLine(0, first.getHeight(), first.getWidth(),
first.getHeight(), paint);
canvas.drawBitmap(second, 0, first.getHeight(), null);
return result;
}
/** 品字形拼接图片 第一张小 第二张大 **/
private Bitmap addTriangleBitmap(Bitmap first, Bitmap second) {
int height = first.getHeight() + second.getHeight();
int width = Math.max(first.getWidth(), second.getWidth());
Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
Canvas canvas = new Canvas(result);
canvas.drawBitmap(first, width / 2 - first.getWidth() / 2, 0, null);
paint.setStrokeWidth(20);
canvas.drawLine(width / 2 - first.getWidth() / 2, first.getHeight(),
width / 2 + first.getWidth() / 2, first.getHeight(), paint);
canvas.drawBitmap(second, 0, first.getHeight(), null);
return result;
}