图片处理时边框的处理以及翻转
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法。 /**
, * 图片与边框组合
, * @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的区域
, * @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;
,, }