如何在Android中使用Canvas合并两个图像?

发布于 2024-12-19 21:36:17 字数 1307 浏览 5 评论 0原文

我想通过重叠创建两个不同图像的组合图像。

对于这个我的代码是

  ImageView image = (ImageView) findViewById(R.id.imageView1);
  Drawable drawableFore = getResources().getDrawable(R.drawable.foreg);
  Drawable drawableBack = getResources().getDrawable(R.drawable.backg);

  Bitmap bitmapFore = ((BitmapDrawable) drawableFore).getBitmap();
  Bitmap bitmapBack = ((BitmapDrawable) drawableBack).getBitmap();

  Bitmap scaledBitmapFore = Bitmap.createScaledBitmap(bitmapFore, 35, 35, true);
  Bitmap scaledBitmapBack = Bitmap.createScaledBitmap(bitmapBack, 45, 45, true);

  Bitmap combineImages = overlay(scaledBitmapBack, scaledBitmapFore);

  image.setImageBitmap(combineImages);

overlay()方法是

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
 try
 {
   Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),  bmp1.getConfig());
   Canvas canvas = new Canvas(bmOverlay);
   canvas.drawBitmap(bmp1, new Matrix(), null);
   canvas.drawBitmap(bmp2, 0, 0, null);
   return bmOverlay;
 } catch (Exception e)
 {
    // TODO: handle exception
  e.printStackTrace();
  return null;
 }
}

情况1:overlay方法在这种情况下返回null。

情况2:但是当我切换图像时,例如使用背景图像在前景中设置,使用前景图像在背景中设置,那么代码可以正常工作。

但我希望第一个案例应该可以正常工作,但事实并非如此。 我不明白为什么会发生这种情况。

请帮忙

I want to create a combined image with two different images by overlapping.

For this My code is

  ImageView image = (ImageView) findViewById(R.id.imageView1);
  Drawable drawableFore = getResources().getDrawable(R.drawable.foreg);
  Drawable drawableBack = getResources().getDrawable(R.drawable.backg);

  Bitmap bitmapFore = ((BitmapDrawable) drawableFore).getBitmap();
  Bitmap bitmapBack = ((BitmapDrawable) drawableBack).getBitmap();

  Bitmap scaledBitmapFore = Bitmap.createScaledBitmap(bitmapFore, 35, 35, true);
  Bitmap scaledBitmapBack = Bitmap.createScaledBitmap(bitmapBack, 45, 45, true);

  Bitmap combineImages = overlay(scaledBitmapBack, scaledBitmapFore);

  image.setImageBitmap(combineImages);

overlay() method is

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
 try
 {
   Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(),  bmp1.getConfig());
   Canvas canvas = new Canvas(bmOverlay);
   canvas.drawBitmap(bmp1, new Matrix(), null);
   canvas.drawBitmap(bmp2, 0, 0, null);
   return bmOverlay;
 } catch (Exception e)
 {
    // TODO: handle exception
  e.printStackTrace();
  return null;
 }
}

case 1 :overlay method returns null in this case.

case 2: But when I switch images like I use background image for setting in foreground and foreground image for setting in background then code works fine.

but I want the first case should work properly but it is not.
I am not getting why this is happening.

Please Help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-12-26 21:36:17

我认为会发生这种情况,因为第二个位图的尺寸更大。所以试试这个:

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
 try
 {
   int maxWidth = (bmp1.getWidth() > bmp2.getWidth() ? bmp1.getWidth() : bmp2.getWidth());
   int maxHeight = (bmp1.getHeight() > bmp2.getHeight() ? bmp1.getHeight() : bmp2.getHeight());
   Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight,  bmp1.getConfig());
   Canvas canvas = new Canvas(bmOverlay);
   canvas.drawBitmap(bmp1, 0, 0, null);
   canvas.drawBitmap(bmp2, 0, 0, null);
   return bmOverlay;

 } catch (Exception e)
 {
    // TODO: handle exception
  e.printStackTrace();
  return null;
 }
}

I think it happens, because the 2nd bitmap is bigger in size. So try this:

public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
 try
 {
   int maxWidth = (bmp1.getWidth() > bmp2.getWidth() ? bmp1.getWidth() : bmp2.getWidth());
   int maxHeight = (bmp1.getHeight() > bmp2.getHeight() ? bmp1.getHeight() : bmp2.getHeight());
   Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight,  bmp1.getConfig());
   Canvas canvas = new Canvas(bmOverlay);
   canvas.drawBitmap(bmp1, 0, 0, null);
   canvas.drawBitmap(bmp2, 0, 0, null);
   return bmOverlay;

 } catch (Exception e)
 {
    // TODO: handle exception
  e.printStackTrace();
  return null;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文