標籤:進度條 tar tac png length sse mat tools enter
圖片處理時,有時需要為圖片加一些邊框,下面介紹一種為圖片添加簡單邊框的方法。
基本思路是:將邊框圖片裁剪成八張小圖片(圖片大小最好一致,不然後面處理會很麻煩),分別對應左上方,左邊,左下角,下邊,右下角,右邊,右上方,上邊,其中左右上下只需要一個有效長度,就像重寫水平進度條一樣,只需要一個有效長度,然後平鋪,就達到了最後想要的效果,不錯,左右上下邊採用的也是這樣的思路。也可以將八張圖片組合在一起,然後讀取整張圖片,用代碼裁剪,下面會給出相應的代碼。下面的代碼主要是給出第一種方法,後一種給出代碼,有興趣的可以自己試試。注意圖片不要放到drawable目錄下面,因為螢幕解析度會影響圖片的大小,所以最好是放在assets目錄裡面。下面代碼為了簡便所以沒有那樣做。後面一篇還會講到另一種添加邊框圖片的方法。
下面貼圖片:
原圖片:
處理後:
代碼(res參數為上面所說的八個邊框組合圖片資源):
[java] view plain copy
- /**
- * 圖片與邊框組合
- * @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影像處理系列之二--圖片旋轉、縮放、反轉的圖片縮放。
[java] view plain copy
- /**
- * 截取圖片的中間的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影像處理系列之四-- 給圖片添加邊框(上)