圖片合成
/**
* 圖片合成
* @param bitmap
* @return
*/
private Bitmap createBitmap( Bitmap src, Bitmap watermark ) {
if( src == null ) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
int ww = watermark.getWidth();
int wh = watermark.getHeight();
//create the new blank bitmap
Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );//建立一個新的和SRC長度寬度一樣的位元影像
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0座標開始畫入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角畫入浮水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//儲存
//store
cv.restore();//儲存
return newb;
}
圖片圓角
/**
* 圖片圓角
* @param bitmap
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 12;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
圖片縮放、翻轉和旋轉
/**
* 縮放、翻轉和旋轉圖片
* @param bmpOrg
* @param rotate
* @return
*/
private android.graphics.Bitmap gerZoomRotateBitmap(
android.graphics.Bitmap bmpOrg, int rotate) {
// 擷取圖片的原始的大小
int width = bmpOrg.getWidth();
int height = bmpOrg.getHeight();
int newWidth = 300;
int newheight = 300;
// 定義縮放的高和寬的比例
float sw = ((float) newWidth) / width;
float sh = ((float) newheight) / height;
// 建立操作圖片的用的Matrix對象
android.graphics.Matrix matrix = new android.graphics.Matrix();
// 縮放翻轉圖片的動作
// sw sh的絕對值為綻放寬高的比例,sw為負數表示X方向翻轉,sh為負數表示Y方向翻轉
matrix.postScale(sw, sh);
// 旋轉30*
matrix.postRotate(rotate);
//建立一個新的圖片
android.graphics.Bitmap resizeBitmap = android.graphics.Bitmap
.createBitmap(bmpOrg, 0, 0, width, height, matrix, true);
return resizeBitmap;
}