Code example for Bitmap

Methods: getConfiggetDensitygetHeightgetWidthrecycle

0
	} 
 
	public static Bitmap resizeAndCrop(Bitmap source, int newWidth,
			int newHeight) {
		Matrix matrix = new Matrix();
		int originalWidth = source.getWidth();
		int originalHeight = source.getHeight();
		float scale = Math.max((float) newWidth / originalWidth,
				(float) newHeight / originalHeight);
		matrix.setScale(scale, scale);
		// RectF rect = new RectF(0, 0, originalWidth, originalHeight); 
		// matrix.mapRect(rect); 
		Bitmap bitmap = Bitmap.createBitmap(newWidth, newHeight,
				source.getConfig());
		Canvas canvas = new Canvas();
		canvas.concat(matrix);
		canvas.setBitmap(bitmap);
		bitmap.setDensity(source.getDensity());
		Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
		if (!matrix.rectStaysRect()) {
			paint.setAntiAlias(true);
		} 
 
		int tmpWidth = Math.round(newWidth / scale);
		int tmpHeight = Math.round(newHeight / scale);
		int marginLeft = (originalWidth - tmpWidth) / 2;
		int marginTop = (originalHeight - tmpHeight) / 2;
		Rect srcBound = new Rect(marginLeft, marginTop, originalWidth
				- marginLeft, originalHeight - marginTop);
		Log.d(LOG_TAG, "marginLeft " + marginLeft + " marginTop " + marginTop);
		RectF targetBound = new RectF(0, 0, tmpWidth, tmpHeight);
		canvas.drawBitmap(source, srcBound, targetBound, paint);
		source.recycle();
		return bitmap;
	} 
 
	public static Bitmap cropCenter(Bitmap source, int targetWidth,
			int targetHeight) {