Code example for Matrix

Methods: createBitmap, mapRect, postScale, reset, setRectToRect

0
	 
	/** 
	 * Resize bitmap to expected size 
	 */ 
	public Bitmap resizeBitmapToExpectedSize(Bitmap bmp) {
		Matrix m = new Matrix();
		int w = bmp.getWidth();
		int h = bmp.getHeight();
		RectF srcRect = new RectF(0, 0, w, h);
		RectF dstRect = new RectF(0, 0, CAPTURE_IMAGE_WIDTH, CAPTURE_IMAGE_HEIGHT);
		 
		m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
		m.mapRect(dstRect, srcRect);
		int dstW = (int) dstRect.width();
		int dstH = (int) dstRect.height();
		 
		Log.v(TAG, "Resized bitmap:w<" + Integer.toString(dstW) + ">,h=<" + Integer.toString(dstH) + ">");
		 
		/* 
		 * Matrix only accept scale. 
		 * Here to resize bitmap I do not need to involve translation factor. 
		 */ 
		m.reset();
		m.postScale(dstW/(float)w, dstH/(float)h);
		return Bitmap.createBitmap(bmp, 0, 0, w, h, m, true);
	} 
 
}