Android之圖片處理(圖片合成、圖片圓角、圖片翻轉、圖片縮放)


圖片合成

/**
* 圖片合成
* @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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM