private static final float DISPLAY_ICON_SIZE = 49 / 1.2f;
public static Drawable mergeDrawable(Context context, Drawable backDrawable, int id, int iconSize){
Bitmap backBitmap = DrawableToBitmap(context, backDrawable);
backBitmap = createBitmap(backBitmap);
Bitmap foreBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(context.getResources(), id));
int width = backBitmap.getWidth() > foreBitmap.getWidth() ? backBitmap.getWidth() : foreBitmap.getWidth();
Bitmap bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(backBitmap, (iconSize - DisplayUtil.dp2px(context, DISPLAY_ICON_SIZE)) / 2f - 2, (iconSize - DisplayUtil.dp2px(context, DISPLAY_ICON_SIZE) - foreBitmap.getHeight()) / 2f + 7, null);
canvas.drawBitmap(foreBitmap, (iconSize - foreBitmap.getWidth()) / 2f, (iconSize + DisplayUtil.dp2px(context, DISPLAY_ICON_SIZE) - foreBitmap.getHeight()) / 2f, null);
backBitmap.recycle();
foreBitmap.recycle();
Drawable drawable = new BitmapDrawable(bitmap);
return drawable;
}
private static Bitmap DrawableToBitmap(Context context, Drawable drawable) {
// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, DisplayUtil.dp2px(context, DISPLAY_ICON_SIZE), DisplayUtil.dp2px(context, DISPLAY_ICON_SIZE));
// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
public static Bitmap createBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap faceIconGreyBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(faceIconGreyBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bitmap, 0, 0, paint);
return faceIconGreyBitmap;
}
如何将2张图上下融合成1张
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...