android 多个canvas,如何使用Canvas在Android中合并两个图像?

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

为此,我的代码是

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:但是当我切换图像时,我使用背景图像设置前景,前景图像设置为背景,然后代码工作正常.

但我希望第一种情况应该正常,但事实并非如此.

我不明白为什么会发生这种情况.

请帮忙

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值