下面列出了android.graphics.Matrix#postRotate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Matrix getMatrixFromClip(
int width,
int height,
RectF bounds,
RectF normalizedClip,
float rotation) {
Matrix matrix = new Matrix();
RectF dstRect = new RectF();
matrix.setTranslate(width * -.5f, height * -.5f);
matrix.postRotate(rotation);
matrix.mapRect(dstRect, new RectF(0f, 0f, width, height));
float dw = dstRect.width();
float dh = dstRect.height();
float scale = bounds.width() / (normalizedClip.width() * dw);
matrix.postScale(scale, scale);
matrix.postTranslate(
bounds.centerX() -
(normalizedClip.centerX() - .5f) * dw * scale,
bounds.centerY() -
(normalizedClip.centerY() - .5f) * dh * scale);
return matrix;
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
if (null != imageDrawable) {
final int dHeight = imageDrawable.getIntrinsicHeight();
final int dWidth = imageDrawable.getIntrinsicWidth();
/**
* We need to set the width/height of the ImageView so that it is
* square with each side the size of the largest drawable dimension.
* This is so that it doesn't clip when rotated.
*/
ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
lp.width = lp.height = Math.max(dHeight, dWidth);
mHeaderImage.requestLayout();
/**
* We now rotate the Drawable so that is at the correct rotation,
* and is centered.
*/
mHeaderImage.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
mHeaderImage.setImageMatrix(matrix);
}
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
if (null != imageDrawable) {
final int dHeight = imageDrawable.getIntrinsicHeight();
final int dWidth = imageDrawable.getIntrinsicWidth();
/**
* We need to set the width/height of the ImageView so that it is
* square with each side the size of the largest drawable dimension.
* This is so that it doesn't clip when rotated.
*/
ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
lp.width = lp.height = Math.max(dHeight, dWidth);
mHeaderImage.requestLayout();
/**
* We now rotate the Drawable so that is at the correct rotation,
* and is centered.
*/
mHeaderImage.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
mHeaderImage.setImageMatrix(matrix);
}
}
private Bitmap rotateImage(final Bitmap bitmap) {
if (bitmap == null) {
return null;
}
Bitmap rotatedBitmap = bitmap;
try {
int orientation = getImageOrientation();
if (orientation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
bitmap.recycle();
}
} catch (IOException e) {
e.printStackTrace();
}
return rotatedBitmap;
}
/**
* 指定した角度回転させたBitmapを生成して返す
* @param bitmap
* @param rotation
* @return
*/
@Nullable
public static Bitmap rotateBitmap(
@Nullable final Bitmap bitmap, final int rotation) {
if (DEBUG) Log.v(TAG, String.format("rotateBitmap:%d", rotation));
Bitmap newBitmap = null;
if (bitmap != null) {
final int width = bitmap.getWidth();
final int height = bitmap.getHeight();
final Matrix matrix = new Matrix();
matrix.postRotate(rotation);
newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
if (DEBUG) Log.v(TAG, "rotateBitmap:bitmap=" + bitmap + " newBitmap=" + newBitmap);
return newBitmap;
}
/**
* 旋转图片
*
* @param angle 被旋转角度
* @param bitmap 图片对象
* @return 旋转后的图片
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bitmap;
}
if (bitmap != returnBm) {
bitmap.recycle();
}
return returnBm;
}
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `mTextureView` is fixed.
*
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
private void configureTransform(final int viewWidth, final int viewHeight) {
final Activity activity = getActivity();
if (null == textureView || null == previewSize || null == activity) {
return;
}
final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
final Matrix matrix = new Matrix();
final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
final float centerX = viewRect.centerX();
final float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
final float scale =
Math.max(
(float) viewHeight / previewSize.getHeight(),
(float) viewWidth / previewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
textureView.setTransform(matrix);
}
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `mTextureView` is fixed.
*
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
private void configureTransform(final int viewWidth, final int viewHeight) {
final Activity activity = getActivity();
if (null == textureView || null == previewSize || null == activity) {
return;
}
final int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
final Matrix matrix = new Matrix();
final RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
final RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
final float centerX = viewRect.centerX();
final float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
final float scale =
Math.max(
(float) viewHeight / previewSize.getHeight(),
(float) viewWidth / previewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
textureView.setTransform(matrix);
}
/*** 将图片按照某个角度进行旋转
** @param bm
* 需要旋转的图片
* @param degree
* 旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}
/**
* Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
* This method should be called after the camera preview size is determined in
* setUpCameraOutputs and also the size of `mTextureView` is fixed.
*
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
private void configureTransform(int viewWidth, int viewHeight) {
Activity activity = getActivity();
if (null == mTextureView || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}
public Matrix getRotateMatrix() {
// By default this is an identity matrix.
Matrix matrix = new Matrix();
if (mRotation != 0) {
// We want to do the rotation at origin, but since the bounding
// rectangle will be changed after rotation, so the delta values
// are based on old & new width/height respectively.
int cx = mBitmap.getWidth() / 2;
int cy = mBitmap.getHeight() / 2;
matrix.preTranslate(-cx, -cy);
matrix.postRotate(mRotation);
matrix.postTranslate(getWidth() / 2, getHeight() / 2);
}
return matrix;
}
/**
* 将图片按照指定的角度进行旋转
*
* @param bitmap 需要旋转的图片
* @param degree 指定的旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
if (!bitmap.isRecycled()) {
bitmap.recycle();
}
return newBitmap;
}
/**
* 旋转bitmap
* @param bitmap
* @param degress 旋转角度
* @param needRecycle
* @return
*/
public static Bitmap rotateBitmap(Bitmap bitmap, int degress, boolean needRecycle) {
Matrix m = new Matrix();
m.postRotate(degress);
Bitmap bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
if(needRecycle) {
bitmap.recycle();
}
return bm;
}
private void invalidate() {
final Matrix matrix = new Matrix();
final int cx = this.mBitmapWidth / 2;
final int cy = this.mBitmapHeight / 2;
matrix.preTranslate(-cx, -cy);
matrix.postRotate(this.mRotation);
matrix.postTranslate(cx, cx);
final RectF rect = new RectF(0, 0, this.mBitmapWidth,
this.mBitmapHeight);
matrix.mapRect(rect);
this.mWidth = (int) rect.width();
this.mHeight = (int) rect.height();
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable) {
if (null != imageDrawable) {
final int dHeight = imageDrawable.getIntrinsicHeight();
final int dWidth = imageDrawable.getIntrinsicWidth();
/**
* We need to set the width/height of the ImageView so that it is
* square with each side the size of the largest drawable dimension.
* This is so that it doesn't clip when rotated.
*/
ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
lp.width = lp.height = Math.max(dHeight, dWidth);
mHeaderImage.requestLayout();
/**
* We now rotate the Drawable so that is at the correct rotation,
* and is centered.
*/
mHeaderImage.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postTranslate((lp.width - dWidth) / 2f,
(lp.height - dHeight) / 2f);
matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f,
lp.height / 2f);
mHeaderImage.setImageMatrix(matrix);
}
}
@Override
protected void onLoadingDrawableSet(Drawable imageDrawable)
{
if (null != imageDrawable)
{
final int dHeight = imageDrawable.getIntrinsicHeight();
final int dWidth = imageDrawable.getIntrinsicWidth();
/**
* We need to set the width/height of the ImageView so that it is
* square with each side the size of the largest drawable dimension.
* This is so that it doesn't clip when rotated.
*/
ViewGroup.LayoutParams lp = mHeaderImage.getLayoutParams();
lp.width = lp.height = Math.max(dHeight, dWidth);
mHeaderImage.requestLayout();
/**
* We now rotate the Drawable so that is at the correct rotation,
* and is centered.
*/
mHeaderImage.setScaleType(ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.postTranslate((lp.width - dWidth) / 2f, (lp.height - dHeight) / 2f);
matrix.postRotate(getDrawableRotationAngle(), lp.width / 2f, lp.height / 2f);
mHeaderImage.setImageMatrix(matrix);
}
}
public Matrix getRotateMatrix() {
// By default this is an identity matrix.
Matrix matrix = new Matrix();
if (mRotation != 0) {
// We want to do the rotation at origin, but since the bounding
// rectangle will be changed after rotation, so the delta values
// are based on old & new width/height respectively.
int cx = mBitmap.getWidth() / 2;
int cy = mBitmap.getHeight() / 2;
matrix.preTranslate(-cx, -cy);
matrix.postRotate(mRotation);
matrix.postTranslate(getWidth() / 2, getHeight() / 2);
}
return matrix;
}
private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor << 1;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
Matrix mtx = new Matrix();
mtx.postRotate(90);
// Rotating Bitmap
Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);
if (rotatedBMP != bitmap)
bitmap.recycle();
mImageView.setImageBitmap(rotatedBMP);
try {
sendPhoto(rotatedBMP);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Bitmap rotate(Bitmap bitmap, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
}
/**
* Sets a Bitmap and initializes the image rotation according to the EXIT data.
* <p>
* The EXIF can be retrieved by doing the following:
* <code>ExifInterface exif = new ExifInterface(path);</code>
*
* @param bitmap the original bitmap to set; if null, this
* @param exif the EXIF information about this bitmap; may be null
*/
public void setImageBitmap(Bitmap bitmap, ExifInterface exif) {
if (bitmap == null) {
return;
}
if (exif == null) {
setImageBitmap(bitmap);
return;
}
final Matrix matrix = new Matrix();
final int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
int rotate = -1;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
}
if (rotate == -1) {
setImageBitmap(bitmap);
} else {
matrix.postRotate(rotate);
final Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap,
0,
0,
bitmap.getWidth(),
bitmap.getHeight(),
matrix,
true);
setImageBitmap(rotatedBitmap);
bitmap.recycle();
}
}