Android有时候会遇到需要两张图片叠加起来的效果,现记录如下:

/*
*此方法分别传入两个bitmap对象,一个为底图(背景图background),
*另一个则位于其上面(前景图foreground),若上面的bitmap是不透明的话,
*就会完全遮住下面的bitmap,那么保存为图片后,就只能看到位于上面的bitmap的信息,
*图片的大小为两个bitmap叠加的大小。
*/ private Bitmap toConformBitmap(Bitmap background, Bitmap foreground) {if( background == null ) {return null;}int bgWidth = background.getWidth();int bgHeight = background.getHeight();//create the new blank bitmap 创建一个新的和SRC长度宽度一样的位图Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);Canvas cv = new Canvas(newbmp);//draw bg into//在 0,0坐标开始画入bgcv.drawBitmap(background, 0, 0, null);//draw fg intoPaint paint = new Paint();paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//在 0,0坐标开始画入fg ,可以从任意位置画入cv.drawBitmap(foreground, 0, 1000, paint);//save all clippaint.setXfermode(null);return newbmp;}
/*
*保存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;}

另外有从其他博客上看到的方法,包括上下叠加,左右拼接和上下拼接。

    /** 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准* @param backBitmap 在底部的位图* @param frontBitmap 盖在上面的位图* @return*/public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap frontBitmap) {if (backBitmap == null || backBitmap.isRecycled() || frontBitmap == null || frontBitmap.isRecycled()) {Log.e(TAG, "backBitmap=" + backBitmap + ";frontBitmap=" + frontBitmap);return null;}Bitmap bitmap = backBitmap.copy(Config.ARGB_8888, true);Canvas canvas = new Canvas(bitmap);Rect baseRect  = new Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight());Rect frontRect = new Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight());canvas.drawBitmap(frontBitmap, frontRect, baseRect, null);return bitmap;}/*** 把两个位图覆盖合成为一个位图,左右拼接* @param leftBitmap * @param rightBitmap * @param isBaseMax 是否以宽度大的位图为准,true则小图等比拉伸,false则大图等比压缩* @return*/public static Bitmap mergeBitmap_LR(Bitmap leftBitmap, Bitmap rightBitmap, boolean isBaseMax) {if (leftBitmap == null || leftBitmap.isRecycled() || rightBitmap == null || rightBitmap.isRecycled()) {JDLog.logError(TAG, "leftBitmap=" + leftBitmap + ";rightBitmap=" + rightBitmap);return null;}int height = 0; // 拼接后的高度,按照参数取大或取小if (isBaseMax) {height = leftBitmap.getHeight() > rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();} else {height = leftBitmap.getHeight() < rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();}// 缩放之后的bitmapBitmap tempBitmapL = leftBitmap;Bitmap tempBitmapR = rightBitmap;if (leftBitmap.getHeight() != height) {tempBitmapL = Bitmap.createScaledBitmap(leftBitmap, (int)(leftBitmap.getWidth()*1f/leftBitmap.getHeight()*height), height, false);} else if (rightBitmap.getHeight() != height) {tempBitmapR = Bitmap.createScaledBitmap(rightBitmap, (int)(rightBitmap.getWidth()*1f/rightBitmap.getHeight()*height), height, false);}// 拼接后的宽度int width = tempBitmapL.getWidth() + tempBitmapR.getWidth();// 定义输出的bitmapBitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);// 缩放后两个bitmap需要绘制的参数Rect leftRect = new Rect(0, 0, tempBitmapL.getWidth(), tempBitmapL.getHeight());Rect rightRect  = new Rect(0, 0, tempBitmapR.getWidth(), tempBitmapR.getHeight());// 右边图需要绘制的位置,往右边偏移左边图的宽度,高度是相同的Rect rightRectT  = new Rect(tempBitmapL.getWidth(), 0, width, height);canvas.drawBitmap(tempBitmapL, leftRect, leftRect, null);canvas.drawBitmap(tempBitmapR, rightRect, rightRectT, null);return bitmap;}/*** 把两个位图覆盖合成为一个位图,上下拼接* @param leftBitmap * @param rightBitmap * @param isBaseMax 是否以高度大的位图为准,true则小图等比拉伸,false则大图等比压缩* @return*/public static Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {if (topBitmap == null || topBitmap.isRecycled() || bottomBitmap == null || bottomBitmap.isRecycled()) {JDLog.logError(TAG, "topBitmap=" + topBitmap + ";bottomBitmap=" + bottomBitmap);return null;}int width = 0;if (isBaseMax) {width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();} else {width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();}Bitmap tempBitmapT = topBitmap;Bitmap tempBitmapB = bottomBitmap;if (topBitmap.getWidth() != width) {tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int)(topBitmap.getHeight()*1f/topBitmap.getWidth()*width), false);} else if (bottomBitmap.getWidth() != width) {tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int)(bottomBitmap.getHeight()*1f/bottomBitmap.getWidth()*width), false);}int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());Rect bottomRect  = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());Rect bottomRectT  = new Rect(0, tempBitmapT.getHeight(), width, height);canvas.drawBitmap(tempBitmapT, topRect, topRect, null);canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);return bitmap;}

此方法只是两张图片的简单叠加拼接,而我们可能还需要一些更加复杂的叠加效果,所以android提供了16种叠加效果,请看下一篇!
												

android图片叠加方法相关推荐

  1. android图片处理方法(不断收集中)

    //压缩图片大小 public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArr ...

  2. Android图片压缩方法总结

    本文总结Android应用开发中三种常见的图片压缩方法,分别是:质量压缩法.比例压缩法(根据路径获取图片并压缩)和比例压缩法(根据Bitmap图片压缩). 第一:质量压缩方法: ? 1 2 3 4 5 ...

  3. android 图片叠加xml,Android实现图片叠加效果的两种方法

    本文实例讲述了Android实现图片叠加效果的两种方法.,具体如下: 效果图: 第一种: 第二种: 第一种是通过canvas画出来的效果: public void first(View v) { // ...

  4. Android图片缩放方法

    方法1:按固定比例进行缩放 在开发图片浏览器等 软件是,很多时候要显示图片的缩略图,而一般情况下,我们要将图片按照固定大小取缩略图,一般取缩略图的方法是使用BitmapFactory的 decodeF ...

  5. android 图片叠加xml,Drawable子类之——LayerDrawable (图层叠加)

    本文出自 "阿敏其人" 简书博客,转载或引用请注明出处. LayerDrawable对应的XML的根元素是,,它使一种层次化显示的Drawable集合.也就说,可以通过显示由多个D ...

  6. 三种Android图片压缩方法 压缩到指定大小

    一.图片质量压缩 /*** 质量压缩方法* @param image* @return*/ public static Bitmap compressImage(Bitmap image) {Byte ...

  7. android 图片压缩方法分析

    demo 参考会放在末尾 1 质量压缩 先看截图吧,手机截图可能有点不优雅 通过压缩前后数据可以知道, 质量压缩之后不会减少图片的像素,它是在保持像素的前提下改变图片的位深及透明度,来达到压缩图片的目 ...

  8. Shader学习12——简易图片叠加

    看到蛮牛有人想要两个带透明通道的图片叠加,就是最简单的纹理混合,想想其实实现起来应该很简单,但是搜了一下还真没搜到,这里简单实现一下,要求底图需要是不透明的: image.png image.png ...

  9. android 图片处理大全

    http://06peng.com/read.php/52.htm 原帖地址--非常好- Android 图片处理方法大全 Android编程 , 评论(3) , 引用(0) , 阅读(1577) 大 ...

最新文章

  1. WPF学习12:基于MVVM Light 制作图形编辑工具(3)
  2. JAVA Feign
  3. android模拟器默认位置的修改
  4. Project2010简易操作指南[转]
  5. java interface 默认_Java8 接口interface默认方法
  6. 即将发布的 JDK 11 包含了什么?
  7. jquery java aes_[代码全屏查看]-java、js之间使用AES加密通信数据
  8. Spark修炼之道(高级篇)——Spark源码阅读:第九节 Task执行成功时的结果处理...
  9. python : os.path 相关操作
  10. CVPR2022 Oral | CosFace、ArcFace的大统一升级,AdaFace解决低质量图像人脸识
  11. python中的深拷贝和浅拷贝
  12. 网易邮箱服务器怎么注册,免费网易域名邮箱申请教程
  13. ArcGIS基本操作
  14. 万年历程序中十二生肖排列顺序及算法
  15. latex 标题chapter section里的英文和数字不加粗
  16. Java —— 自定义JSR303校验
  17. 数字营销浪潮下,企业如何打赢流量反欺诈攻防战?
  18. 产品化机器学习的一些思考
  19. css–sprit_CSS速记与速记–使用哪个
  20. linux下通过串口ftp,eftp简单文件传输工具支持串口、网络、Windows、Linux、单片机平台-博客...

热门文章

  1. 弘辽科技:零食市场内卷化 洽洽的功守道
  2. linux,debian系统安装录制视频软件simplescreenRecorder
  3. iOS第三方开源库的吐槽和备忘 - 王培
  4. 《惢客创业日记》2021.06.28-30(周一)防骗的终极解决方案
  5. [RK3399][Android7.1] 移植笔记 --- 9.7寸eDP显示屏添加
  6. Selenium Gird下文件上传问题的解决(WebUI自动化测试)
  7. HDU - 1546 Idiomatic Phrases Game
  8. jasypt加密敏感配置信息出现Encryption raised an exception
  9. 欧几里德算法(Euclidean algorithm)
  10. 【2015NOIP模拟】【Ocd】【Mancity】【Captcha】10.31总结