下面列出了怎么用android.graphics.Matrix.ScaleToFit的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Configures the necessary {@link Matrix} transformation.
* This method should be called after the camera preview size is determined.
*
* @param displayRotation current rotation of default display
* @param viewWidth The width of `mTextureView`
* @param viewHeight The height of `mTextureView`
*/
void configureTransform(
int displayRotation, int viewWidth, int viewHeight, Size previewSize) {
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == displayRotation || Surface.ROTATION_270 == displayRotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / previewSize.getHeight(),
(float) viewWidth / previewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (displayRotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == displayRotation) {
matrix.postRotate(180, centerX, centerY);
}
setTransform(matrix);
}
private void calculateImageMatrixFromScale(float scale, @NonNull Matrix matrix) {
matrix.reset();
Drawable drawable = view.getDrawable();
if (drawable != null && maxImageSize != 0) {
// First make sure our image respects mMaxImageSize.
RectF drawableBounds = tmpRectF1;
RectF imageBounds = tmpRectF2;
drawableBounds.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
imageBounds.set(0, 0, maxImageSize, maxImageSize);
matrix.setRectToRect(drawableBounds, imageBounds, ScaleToFit.CENTER);
// Then scale it as requested.
matrix.postScale(scale, scale, maxImageSize / 2f, maxImageSize / 2f);
}
}
public void setFraming(final Rect frame, final RectF framePreview, final int displayRotation,
final int cameraRotation, final boolean cameraFlip) {
this.frame = frame;
matrix.setRectToRect(framePreview, new RectF(frame), ScaleToFit.FILL);
matrix.postRotate(-displayRotation, frame.exactCenterX(), frame.exactCenterY());
matrix.postScale(cameraFlip ? -1 : 1, 1, frame.exactCenterX(), frame.exactCenterY());
matrix.postRotate(cameraRotation, frame.exactCenterX(), frame.exactCenterY());
invalidate();
}
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
this.mRect.set((float) this.margin, (float) this.margin, (float) (bounds.width() - this.margin), (float) (bounds.height() - this.margin));
Matrix shaderMatrix = new Matrix();
shaderMatrix.setRectToRect(this.mBitmapRect, this.mRect, ScaleToFit.FILL);
this.bitmapShader.setLocalMatrix(shaderMatrix);
}
public void setFraming(final Rect frame, final RectF framePreview, final int displayRotation,
final int cameraRotation, final boolean cameraFlip) {
this.frame = frame;
matrix.setRectToRect(framePreview, new RectF(frame), ScaleToFit.FILL);
matrix.postRotate(-displayRotation, frame.exactCenterX(), frame.exactCenterY());
matrix.postScale(cameraFlip ? -1 : 1, 1, frame.exactCenterX(), frame.exactCenterY());
matrix.postRotate(cameraRotation, frame.exactCenterX(), frame.exactCenterY());
invalidate();
}
public void setFraming(final Rect frame, final RectF framePreview, final int displayRotation,
final int cameraRotation, final boolean cameraFlip) {
this.frame = frame;
matrix.setRectToRect(framePreview, new RectF(frame), ScaleToFit.FILL);
matrix.postRotate(-displayRotation, frame.exactCenterX(), frame.exactCenterY());
matrix.postScale(cameraFlip ? -1 : 1, 1, frame.exactCenterX(), frame.exactCenterY());
matrix.postRotate(cameraRotation, frame.exactCenterX(), frame.exactCenterY());
invalidate();
}