`
chjmars
  • 浏览: 76058 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Android 缩放图片

阅读更多

/**
 * resize Bitmap
 * 
 * @param bitmap
 * @param newWidth
 * @return
 */
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth) {
	if (bitmap == null)
		return null;
	int w = bitmap.getWidth();
	int h = bitmap.getHeight();

	Log.e("Jarvis", w + "~" + h);

	float temp = ((float) h) / ((float) w);
	int newHeight = (int) (newWidth * temp);
	float scaleWidth = ((float) newWidth) / w;
	float scaleHeight = ((float) newHeight) / h;
	Matrix matrix = new Matrix();
	matrix.postScale(scaleWidth, scaleHeight);
	Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
			true);
	if (!bitmap.isRecycled()) {
		bitmap.recycle();
	}

	return resizedBitmap;
}
/**
 * 放大缩小图片
 * 
 * @param bitmap
 * @param w
 * @param h
 * @return
 */
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
	int width = bitmap.getWidth();
	int height = bitmap.getHeight();
	Matrix matrix = new Matrix();
	float scaleWidht = ((float) w / width);
	float scaleHeight = ((float) h / height);
	matrix.postScale(scaleWidht, scaleHeight);
	Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
			matrix, true);
	return newbmp;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics