将两个Bitmap资源合成一张bitmap

来源:互联网 发布:京东手机抢购软件 编辑:程序博客网 时间:2024/05/16 07:20

很好的方法,可以直接使用

/**
  * 合并两张bitmap为一张
  * @param background
  * @param foreground
  * @return Bitmap
  */
 public static Bitmap combineBitmap(Bitmap background, Bitmap foreground)
       {
  if (background == null)
                {
   return null;
  }
  int bgWidth = background.getWidth();
  int bgHeight = background.getHeight();
  int fgWidth = foreground.getWidth();
  int fgHeight = foreground.getHeight();
  Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);
  Canvas canvas = new Canvas(newmap);
  canvas.drawBitmap(background, 0, 0, null);
  canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2,
    (bgHeight - fgHeight) / 2, null);
  canvas.save(Canvas.ALL_SAVE_FLAG);
  canvas.restore();
  return newmap;
 }

0 0
原创粉丝点击