android整合两个bitmap

有时候会遇到这样的需求,将两个bitmap对象整合并保存为一张图片,代码如下:
private Bitmap toConformBitmap(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();   
         //create the new blank bitmap 创建一个新的和SRC长度宽度一样的位图   
         Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);  
         Canvas cv = new Canvas(newbmp);   
         //draw bg into   
         cv.drawBitmap(background, 0, 0, null);//在 0,0坐标开始画入bg   
         //draw fg into   
         cv.drawBitmap(foreground, 0, 0, null);//在 0,0坐标开始画入fg ,可以从任意位置画入
         //save all clip   
         cv.save(Canvas.ALL_SAVE_FLAG);//保存   
         //store   
         cv.restore();//存储   
         return newbmp;   
    }
    此方法分别传入两个bitmap对象,一个为底图(背景图background),另一个则位于其上面(前景图foreground),若上面的bitmap是不透明的话,就会完全遮住下面的bitmap,那么保存为图片后,就只能看到位于上面的bitmap的信息,图片的大小为两个bitmap叠加的大小。
保存bitmap为一张图片:
private String saveBitmap(Bitmap bitmap) {
        String  imagePath = getApplication().getFilesDir().getAbsolutePath() + "/temp.png";
        
        File file = new File(imagePath);
        if(file.exists()) {
            file.delete();
        }
        try{
            FileOutputStream out = new FileOutputStream(file);
            if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
                out.flush();
                out.close();
            }     
        } catch (Exception e) {
            Toast.makeText(this, "保存失败, 1).show();
            e.printStackTrace();
        }
        return imagePath;

    }


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值