android图片叠加方法
2017-10-17 16:15
1201 查看
Android有时候会遇到需要两张图片叠加起来的效果,现记录如下:
此方法只是两张图片的简单叠加拼接,而我们可能还需要一些更加复杂的叠加效果,所以android提供了16种叠加效果,请看下一篇!
/* *此方法分别传入两个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坐标开始画入bg cv.drawBitmap(background, 0, 0, null); //draw fg into Paint paint = new Paint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); //在 0,0坐标开始画入fg ,可以从任意位置画入 cv.drawBitmap(foreground, 0, 1000, paint); //save all clip paint.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(); } // 缩放之后的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; }
此方法只是两张图片的简单叠加拼接,而我们可能还需要一些更加复杂的叠加效果,所以android提供了16种叠加效果,请看下一篇!
相关文章推荐
- android 图片叠加效果——两种方法
- Android实现图片叠加效果的两种方法
- android 图片叠加效果——两种方法的简介与内容
- android 图片叠加效果——两种方法
- 2012-7-20 android 图片叠加效果——两种方法
- android 图片叠加效果——两种方法
- Android图片处理的的一些方法
- Android下图片处理的的一些方法
- Android中图片缩放方法
- Android裁剪图片最简单方法
- Android --- 图片处理的方法
- Android图片处理的的一些方法 - ORIETech
- Android最简单的图片裁剪方法,非使用系统裁切功能
- Android裁剪图片最简单方法
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)
- Android 获得图片的方法
- android Matrix处理图片原理及方法整理
- android两种使用照相功能获取图片的方法
- Android中获取网络图片的三种方法
- Android下图片处理的的一些方法