android图像处理系列之四--给图片添加边框(上)
来源:互联网 发布:照片装饰软件下载 编辑:程序博客网 时间:2024/04/29 22:22
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法。
基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下角,右边,右上角,上边,其中左右上下只需要一个有效长度,就像重写水平进度条一样,只需要一个有效的长度,然后平铺,就达到了最后想要的效果,不错,左右上下边采用的也是这样的思路。也可以将八张图片组合在一起,然后读取整张图片,用代码裁剪,下面会给出相应的代码。下面的代码主要是给出第一种方法,后一种给出代码,有兴趣的可以自己试试。注意图片不要放到drawable目录下面,因为屏幕分辨率会影响图片的大小,所以最好是放在assets目录里面。下面代码为了简便所以没有那样做。后面一篇还会讲到另一种添加边框图片的方法。
下面贴图片:
原图片:
处理后:
代码(res参数为上面所说的八个边框组合图片资源):
- /**
- * 图片与边框组合
- * @param bm 原图片
- * @param res 边框资源
- * @return
- */
- private Bitmap combinateFrame(Bitmap bm, int[] res)
- {
- Bitmap bmp = decodeBitmap(res[0]);
- // 边框的宽高
- final int smallW = bmp.getWidth();
- final int smallH = bmp.getHeight();
- // 原图片的宽高
- final int bigW = bm.getWidth();
- final int bigH = bm.getHeight();
- int wCount = (int) Math.ceil(bigW * 1.0 / smallW);
- int hCount = (int) Math.ceil(bigH * 1.0 / smallH);
- // 组合后图片的宽高
- int newW = (wCount + 2) * smallW;
- int newH = (hCount + 2) * smallH;
- // 重新定义大小
- Bitmap newBitmap = Bitmap.createBitmap(newW, newH, Config.ARGB_8888);
- Canvas canvas = new Canvas(newBitmap);
- Paint p = new Paint();
- p.setColor(Color.TRANSPARENT);
- canvas.drawRect(new Rect(0, 0, newW, newH), p);
- Rect rect = new Rect(smallW, smallH, newW - smallW, newH - smallH);
- Paint paint = new Paint();
- paint.setColor(Color.WHITE);
- canvas.drawRect(rect, paint);
- // 绘原图
- canvas.drawBitmap(bm, (newW - bigW - 2 * smallW) / 2 + smallW, (newH - bigH - 2 * smallH) / 2 + smallH, null);
- // 绘边框
- // 绘四个角
- int startW = newW - smallW;
- int startH = newH - smallH;
- Bitmap leftTopBm = decodeBitmap(res[0]); // 左上角
- Bitmap leftBottomBm = decodeBitmap(res[2]); // 左下角
- Bitmap rightBottomBm = decodeBitmap(res[4]); // 右下角
- Bitmap rightTopBm = decodeBitmap(res[6]); // 右上角
- canvas.drawBitmap(leftTopBm, 0, 0, null);
- canvas.drawBitmap(leftBottomBm, 0, startH, null);
- canvas.drawBitmap(rightBottomBm, startW, startH, null);
- canvas.drawBitmap(rightTopBm, startW, 0, null);
- leftTopBm.recycle();
- leftTopBm = null;
- leftBottomBm.recycle();
- leftBottomBm = null;
- rightBottomBm.recycle();
- rightBottomBm = null;
- rightTopBm.recycle();
- rightTopBm = null;
- // 绘左右边框
- Bitmap leftBm = decodeBitmap(res[1]);
- Bitmap rightBm = decodeBitmap(res[5]);
- for (int i = 0, length = hCount; i < length; i++)
- {
- int h = smallH * (i + 1);
- canvas.drawBitmap(leftBm, 0, h, null);
- canvas.drawBitmap(rightBm, startW, h, null);
- }
- leftBm.recycle();
- leftBm = null;
- rightBm.recycle();
- rightBm = null;
- // 绘上下边框
- Bitmap bottomBm = decodeBitmap(res[3]);
- Bitmap topBm = decodeBitmap(res[7]);
- for (int i = 0, length = wCount; i < length; i++)
- {
- int w = smallW * (i + 1);
- canvas.drawBitmap(bottomBm, w, startH, null);
- canvas.drawBitmap(topBm, w, 0, null);
- }
- bottomBm.recycle();
- bottomBm = null;
- topBm.recycle();
- topBm = null;
- canvas.save(Canvas.ALL_SAVE_FLAG);
- canvas.restore();
- return newBitmap;
- }
/** * 图片与边框组合 * @param bm 原图片 * @param res 边框资源 * @return */private Bitmap combinateFrame(Bitmap bm, int[] res){Bitmap bmp = decodeBitmap(res[0]);// 边框的宽高final int smallW = bmp.getWidth();final int smallH = bmp.getHeight();// 原图片的宽高final int bigW = bm.getWidth();final int bigH = bm.getHeight();int wCount = (int) Math.ceil(bigW * 1.0 / smallW);int hCount = (int) Math.ceil(bigH * 1.0 / smallH);// 组合后图片的宽高int newW = (wCount + 2) * smallW;int newH = (hCount + 2) * smallH;// 重新定义大小Bitmap newBitmap = Bitmap.createBitmap(newW, newH, Config.ARGB_8888);Canvas canvas = new Canvas(newBitmap);Paint p = new Paint();p.setColor(Color.TRANSPARENT);canvas.drawRect(new Rect(0, 0, newW, newH), p);Rect rect = new Rect(smallW, smallH, newW - smallW, newH - smallH);Paint paint = new Paint();paint.setColor(Color.WHITE);canvas.drawRect(rect, paint);// 绘原图canvas.drawBitmap(bm, (newW - bigW - 2 * smallW) / 2 + smallW, (newH - bigH - 2 * smallH) / 2 + smallH, null);// 绘边框// 绘四个角int startW = newW - smallW;int startH = newH - smallH;Bitmap leftTopBm = decodeBitmap(res[0]); // 左上角Bitmap leftBottomBm = decodeBitmap(res[2]); // 左下角Bitmap rightBottomBm = decodeBitmap(res[4]); // 右下角Bitmap rightTopBm = decodeBitmap(res[6]); // 右上角canvas.drawBitmap(leftTopBm, 0, 0, null);canvas.drawBitmap(leftBottomBm, 0, startH, null);canvas.drawBitmap(rightBottomBm, startW, startH, null);canvas.drawBitmap(rightTopBm, startW, 0, null);leftTopBm.recycle();leftTopBm = null;leftBottomBm.recycle();leftBottomBm = null;rightBottomBm.recycle();rightBottomBm = null;rightTopBm.recycle();rightTopBm = null;// 绘左右边框Bitmap leftBm = decodeBitmap(res[1]);Bitmap rightBm = decodeBitmap(res[5]);for (int i = 0, length = hCount; i < length; i++){int h = smallH * (i + 1);canvas.drawBitmap(leftBm, 0, h, null);canvas.drawBitmap(rightBm, startW, h, null);}leftBm.recycle();leftBm = null;rightBm.recycle();rightBm = null;// 绘上下边框Bitmap bottomBm = decodeBitmap(res[3]);Bitmap topBm = decodeBitmap(res[7]);for (int i = 0, length = wCount; i < length; i++){int w = smallW * (i + 1);canvas.drawBitmap(bottomBm, w, startH, null);canvas.drawBitmap(topBm, w, 0, null);}bottomBm.recycle();bottomBm = null;topBm.recycle();topBm = null;canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore();return newBitmap;}
如果边框是在一张图片里面,下面给出从一张图片取中间200X200的区域。如何类似边框过多,可以将裁剪的信息写入到指定的文件,裁剪时可先将边框图片信息读取出来,然后再裁剪出边框。如果处理的原图片太大,可能会出内存溢出。可先将图片缩小到一定尺寸再处理,具体缩小方法,参见android图像处理系列之二--图片旋转、缩放、反转的图片缩放。
- /**
- * 截取图片的中间的200X200的区域
- * @param bm
- * @return
- */
- private Bitmap cropCenter(Bitmap bm)
- {
- int dstWidth = 200;
- int dstHeight = 200;
- int startWidth = (bm.getWidth() - dstWidth)/2;
- int startHeight = ((bm.getHeight() - dstHeight) / 2);
- Rect src = new Rect(startWidth, startHeight, startWidth + dstWidth, startHeight + dstHeight);
- return dividePart(bm, src);
- }
- /**
- * 剪切图片
- * @param bmp 被剪切的图片
- * @param src 剪切的位置
- * @return 剪切后的图片
- */
- private Bitmap dividePart(Bitmap bmp, Rect src)
- {
- int width = src.width();
- int height = src.height();
- Rect des = new Rect(0, 0, width, height);
- Bitmap croppedImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
- Canvas canvas = new Canvas(croppedImage);
- canvas.drawBitmap(bmp, src, des, null);
- return croppedImage;
- }
/** * 截取图片的中间的200X200的区域 * @param bm * @return */private Bitmap cropCenter(Bitmap bm){int dstWidth = 200; int dstHeight = 200; int startWidth = (bm.getWidth() - dstWidth)/2; int startHeight = ((bm.getHeight() - dstHeight) / 2); Rect src = new Rect(startWidth, startHeight, startWidth + dstWidth, startHeight + dstHeight); return dividePart(bm, src);}/** * 剪切图片 * @param bmp 被剪切的图片 * @param src 剪切的位置 * @return 剪切后的图片 */private Bitmap dividePart(Bitmap bmp, Rect src){int width = src.width();int height = src.height();Rect des = new Rect(0, 0, width, height);Bitmap croppedImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);Canvas canvas = new Canvas(croppedImage);canvas.drawBitmap(bmp, src, des, null);return croppedImage;}
处理后图片(原图片还是上面的图片):
- android图像处理系列之四--给图片添加边框(上)
- android图像处理系列之四--给图片添加边框(上)
- android图像处理系列之四--给图片添加边框(上)
- android图像处理系列之五--给图片添加边框(中)
- android图像处理系列之五--给图片添加边框(中)
- android图像处理系列之五--给图片添加边框(中)
- android图像处理系列之六--给图片添加边框(下)-图片叠加
- android图像处理系列之六--给图片添加边框(下)-图片叠加
- android图像处理系列之六--给图片添加边框(下)-图片叠加
- android给图片添加边框
- 【Android图像处理】图片叠加/给图片添加纹理
- android项目 之 记事本(12) ----- 图片的等比例缩放及给图片添加边框
- android图像处理系列之七--图片涂鸦,水印-图片叠加
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理 (转)
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- 估算表占用的空间
- 《JS基础——实现元素复制》
- android图片特效处理之模糊效果
- php安装gd(freetype、jpeg、png、zlib)
- getClass()
- android图像处理系列之四--给图片添加边框(上)
- thinkcentre 主机从光盘重装系统
- C#base关键字
- Linux completion理解
- cocos2d-x:动作 Actions
- android图像处理系列之五--给图片添加边框(中)
- 百度Geocoding接口(可用于地理编码和逆地理编码)
- loadrunner 脚本录制问题之 ie
- MFC分割窗口防止用户改变窗格的大小