图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法。

基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下角,右边,右上角,上边,其中左右上下只需要一个有效长度,就像重写水平进度条一样,只需要一个有效的长度,然后平铺,就达到了最后想要的效果,不错,左右上下边采用的也是这样的思路。也可以将八张图片组合在一起,然后读取整张图片,用代码裁剪,下面会给出相应的代码。下面的代码主要是给出第一种方法,后一种给出代码,有兴趣的可以自己试试。注意图片不要放到drawable目录下面,因为屏幕分辨率会影响图片的大小,所以最好是放在assets目录里面。下面代码为了简便所以没有那样做。后面一篇还会讲到另一种添加边框图片的方法。

下面贴图片:

原图片:

处理后:

代码(res参数为上面所说的八个边框组合图片资源):

  1. /**
  2. * 图片与边框组合
  3. * @param bm 原图片
  4. * @param res 边框资源
  5. * @return
  6. */
  7. private Bitmap combinateFrame(Bitmap bm, int[] res)
  8. {
  9. Bitmap bmp = decodeBitmap(res[0]);
  10. // 边框的宽高
  11. final int smallW = bmp.getWidth();
  12. final int smallH = bmp.getHeight();
  13. // 原图片的宽高
  14. final int bigW = bm.getWidth();
  15. final int bigH = bm.getHeight();
  16. int wCount = (int) Math.ceil(bigW * 1.0 / smallW);
  17. int hCount = (int) Math.ceil(bigH  * 1.0 / smallH);
  18. // 组合后图片的宽高
  19. int newW = (wCount + 2) * smallW;
  20. int newH = (hCount + 2) * smallH;
  21. // 重新定义大小
  22. Bitmap newBitmap = Bitmap.createBitmap(newW, newH, Config.ARGB_8888);
  23. Canvas canvas = new Canvas(newBitmap);
  24. Paint p = new Paint();
  25. p.setColor(Color.TRANSPARENT);
  26. canvas.drawRect(new Rect(0, 0, newW, newH), p);
  27. Rect rect = new Rect(smallW, smallH, newW - smallW, newH - smallH);
  28. Paint paint = new Paint();
  29. paint.setColor(Color.WHITE);
  30. canvas.drawRect(rect, paint);
  31. // 绘原图
  32. canvas.drawBitmap(bm, (newW - bigW - 2 * smallW) / 2 + smallW, (newH - bigH - 2 * smallH) / 2 + smallH, null);
  33. // 绘边框
  34. // 绘四个角
  35. int startW = newW - smallW;
  36. int startH = newH - smallH;
  37. Bitmap leftTopBm = decodeBitmap(res[0]); // 左上角
  38. Bitmap leftBottomBm = decodeBitmap(res[2]); // 左下角
  39. Bitmap rightBottomBm = decodeBitmap(res[4]); // 右下角
  40. Bitmap rightTopBm = decodeBitmap(res[6]); // 右上角
  41. canvas.drawBitmap(leftTopBm, 0, 0, null);
  42. canvas.drawBitmap(leftBottomBm, 0, startH, null);
  43. canvas.drawBitmap(rightBottomBm, startW, startH, null);
  44. canvas.drawBitmap(rightTopBm, startW, 0, null);
  45. leftTopBm.recycle();
  46. leftTopBm = null;
  47. leftBottomBm.recycle();
  48. leftBottomBm = null;
  49. rightBottomBm.recycle();
  50. rightBottomBm = null;
  51. rightTopBm.recycle();
  52. rightTopBm = null;
  53. // 绘左右边框
  54. Bitmap leftBm = decodeBitmap(res[1]);
  55. Bitmap rightBm = decodeBitmap(res[5]);
  56. for (int i = 0, length = hCount; i < length; i++)
  57. {
  58. int h = smallH * (i + 1);
  59. canvas.drawBitmap(leftBm, 0, h, null);
  60. canvas.drawBitmap(rightBm, startW, h, null);
  61. }
  62. leftBm.recycle();
  63. leftBm = null;
  64. rightBm.recycle();
  65. rightBm = null;
  66. // 绘上下边框
  67. Bitmap bottomBm = decodeBitmap(res[3]);
  68. Bitmap topBm = decodeBitmap(res[7]);
  69. for (int i = 0, length = wCount; i < length; i++)
  70. {
  71. int w = smallW * (i + 1);
  72. canvas.drawBitmap(bottomBm, w, startH, null);
  73. canvas.drawBitmap(topBm, w, 0, null);
  74. }
  75. bottomBm.recycle();
  76. bottomBm = null;
  77. topBm.recycle();
  78. topBm = null;
  79. canvas.save(Canvas.ALL_SAVE_FLAG);
  80. canvas.restore();
  81. return newBitmap;
  82. }

如果边框是在一张图片里面,下面给出从一张图片取中间200X200的区域。如何类似边框过多,可以将裁剪的信息写入到指定的文件,裁剪时可先将边框图片信息读取出来,然后再裁剪出边框。如果处理的原图片太大,可能会出内存溢出。可先将图片缩小到一定尺寸再处理,具体缩小方法,参见android图像处理系列之二--图片旋转、缩放、反转的图片缩放。

  1. /**
  2. * 截取图片的中间的200X200的区域
  3. * @param bm
  4. * @return
  5. */
  6. private Bitmap cropCenter(Bitmap bm)
  7. {
  8. int dstWidth = 200;
  9. int dstHeight = 200;
  10. int startWidth = (bm.getWidth() - dstWidth)/2;
  11. int startHeight = ((bm.getHeight() - dstHeight) / 2);
  12. Rect src = new Rect(startWidth, startHeight, startWidth + dstWidth, startHeight + dstHeight);
  13. return dividePart(bm, src);
  14. }
  15. /**
  16. * 剪切图片
  17. * @param bmp 被剪切的图片
  18. * @param src 剪切的位置
  19. * @return 剪切后的图片
  20. */
  21. private Bitmap dividePart(Bitmap bmp, Rect src)
  22. {
  23. int width = src.width();
  24. int height = src.height();
  25. Rect des = new Rect(0, 0, width, height);
  26. Bitmap croppedImage = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  27. Canvas canvas = new Canvas(croppedImage);
  28. canvas.drawBitmap(bmp, src, des, null);
  29. return croppedImage;
  30. }

处理后图片(原图片还是上面的图片):

最新文章

  1. 疯狂Android讲义 - 学习笔记(五)
  2. 如何穿越到android底层
  3. HDU 4966 GGS-DDU(最小树形图)
  4. linux命令:whereis
  5. 拉格朗日乘子法和KKT条件
  6. android sqlite导入数据
  7. 【Android】去除应用启动时黑屏现象
  8. console中一些不常用的实用方法
  9. Android--应用开发3(Android layout XML属性)
  10. IndexedDB 增删改查 简单的库
  11. VC++读取资源中文件
  12. George and Cards
  13. 转:sublime text快捷键 (很实用的东东)
  14. SpringMVC @ResponseBody 415错误处理
  15. Alamofire源码解读系列(一)之概述和使用
  16. 201521123045java课程设计---定时器
  17. Python_实现json数据的jsonPath(精简版)定位及增删改操作
  18. 并发之volatile关键字
  19. 超链接(空链接-target-title属性)
  20. C# WINFORM的自动更新程序

热门文章

  1. String methods
  2. 解决夸dll返回dynamic无法访问
  3. VS2012恢复默认设置的2种方法
  4. LeetCode(6)ZigZag Conversion
  5. UNIX 是什么?怎么诞生的?
  6. linux系统下,11款常见远程桌面控制软件(转载)
  7. k近邻法(k-nearest neighbor, k-NN)
  8. php八大设计模式之适配器模式
  9. NodeJS学习笔记 (27)实用工具模块-util(ok)
  10. Tp5 的 validate 自动验证