Android将两个bitmap合并
来源:互联网 发布:唐安琪烧伤真相知乎 编辑:程序博客网 时间:2024/06/04 20:10
把两张bitmap覆盖合成为一张图
/** * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准 * @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(); } // 缩放之后的bitmap Bitmap 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(); // 定义输出的bitmap Bitmap 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; }
记录一下
0 0
- Android将两个bitmap合并
- android 屏幕截图时,如何更改截图内容,将两个bitmap合并一起显示图像
- 将两个Bitmap资源合成一张bitmap
- 将两个bitmap整合成一个bitmap
- android整合两个bitmap
- android整合两个bitmap
- android整合两个bitmap
- C# - 把两个bitmap文件合并成一个bitmap文件
- Android:使用Canvas合并Bitmap
- 合并两张bitmap ANDROID
- 将两个线性表合并
- 如何将两个文件合并?
- 将两个有序单链表合并
- 将两个json数组合并
- ios将两个字典合并
- android 将View转为Bitmap
- Android将View转为Bitmap
- Android合并两张bitmap为一张
- Shell commands
- cmd有关jar的几个命令
- 高效的使用 top
- ADT开发中的一些优化设置:代码自动补全等
- Paint的画Text(drawText)最全解析
- Android将两个bitmap合并
- 转战WebApp: 最适合Android开发者的WebApp框架
- github基础操作教程:fork,clone以及pull request
- java 中class相关的问题
- (转)IIS7(Win7)下最简单最强安装多版本PHP支持环境
- Android 开发中涉及到的设计模式
- Linux常用命令大全
- xampp配置虚拟主机
- View 的圆角和边框