下面列出了android.graphics.Matrix#invert ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private @Nullable Rect computeRegionToDecode(
EncodedImage encodedImage, ImageDecodeOptions options) {
if (!(options instanceof FrescoVitoImageDecodeOptions)) {
return null;
}
FrescoVitoImageDecodeOptions frescoVitoOptions = (FrescoVitoImageDecodeOptions) options;
Rect regionToDecode = new Rect();
Matrix matrix = new Matrix();
frescoVitoOptions.scaleType.getTransform(
matrix,
frescoVitoOptions.parentBounds,
encodedImage.getWidth(),
encodedImage.getHeight(),
frescoVitoOptions.focusPoint.x,
frescoVitoOptions.focusPoint.y);
matrix.invert(matrix);
RectF tempRectangle = new RectF(frescoVitoOptions.parentBounds);
matrix.mapRect(tempRectangle);
tempRectangle.round(regionToDecode);
return regionToDecode;
}
/**
* Returns a matrix that maps points from the crop on to the visible image.
* <p>
* i.e. if a mapped point is in bounds, then the point is on the visible image.
*/
@Nullable Matrix imageMatrixRelativeToCrop() {
EditorElement mainImage = getMainImage();
if (mainImage == null) return null;
Matrix matrix1 = new Matrix(imageCrop.getLocalMatrix());
matrix1.preConcat(cropEditorElement.getLocalMatrix());
matrix1.preConcat(cropEditorElement.getEditorMatrix());
Matrix matrix2 = new Matrix(mainImage.getLocalMatrix());
matrix2.preConcat(mainImage.getEditorMatrix());
matrix2.preConcat(imageCrop.getLocalMatrix());
Matrix inverse = new Matrix();
matrix2.invert(inverse);
inverse.preConcat(matrix1);
return inverse;
}
/**
* 计算点除以矩阵的值
*
* matrix.mapPoints(unknownPoint) -> point
* 已知point和matrix,求unknownPoint的值.
*
* @param point
* @param matrix
* @return unknownPoint
*/
public static float[] inverseMatrixPoint(float[] point, Matrix matrix) {
if (point != null && matrix != null) {
float[] dst = new float[2];
//计算matrix的逆矩阵
Matrix inverse = matrixTake();
matrix.invert(inverse);
//用逆矩阵变换point到dst,dst就是结果
inverse.mapPoints(dst, point);
//清除临时变量
matrixGiven(inverse);
return dst;
} else {
return new float[2];
}
}
@Override
public void applyMatrixForData(Matrix matrix) {
super.applyMatrixForData(matrix);
for (DrawElement element : elements) {
// Before applying data matrix, it should transfer the display matrices to data.
// After that, it should revert back to original display matrix.
Matrix parentInvertDisplay = new Matrix(getInvertedDisplayMatrix());
Matrix originalDisplay = new Matrix(element.getDisplayMatrix());
originalDisplay.postConcat(parentInvertDisplay);
Matrix originalInvertDisplay = new Matrix();
originalDisplay.invert(originalInvertDisplay);
element.applyDisplayMatrixToData();
element.applyMatrixForData(matrix);
element.applyMatrixForData(originalInvertDisplay);
element.getDisplayMatrix().postConcat(originalDisplay);
element.updateBoundingBox();
}
recalculateBoundingBox();
}
/**
* 计算点除以矩阵的值
*
* matrix.mapPoints(unknownPoint) -> point
* 已知point和matrix,求unknownPoint的值.
*
* @return unknownPoint
*/
public static float[] inverseMatrixPoint(float[] point, Matrix matrix) {
if (point != null && matrix != null) {
float[] dst = new float[2];
//计算matrix的逆矩阵
Matrix inverse = matrixTake();
matrix.invert(inverse);
//用逆矩阵变换point到dst,dst就是结果
inverse.mapPoints(dst, point);
//清除临时变量
matrixGiven(inverse);
return dst;
} else {
return new float[2];
}
}
static @NonNull AnimationMatrix animate(@NonNull Matrix from, @NonNull Matrix to, @Nullable Runnable invalidate) {
if (invalidate == null) {
return NULL;
}
Matrix undo = new Matrix();
boolean inverted = to.invert(undo);
if (inverted) {
undo.preConcat(from);
}
if (inverted && !undo.isIdentity()) {
AnimationMatrix animationMatrix = new AnimationMatrix(undo, invalidate);
animationMatrix.start(interpolator);
return animationMatrix;
} else {
return NULL;
}
}
private void calcScreenMatrix() {
mScreenMatrix = new Matrix();
float tileSize = Math.min(mScreenWidth / mGameWidth, mScreenHeight / mGameHeight);
mScreenMatrix.postTranslate(0.5f, 0.5f);
mScreenMatrix.postScale(tileSize, tileSize);
float paddingLeft = (mScreenWidth - (tileSize * mGameWidth)) / 2f;
float paddingBottom = (mScreenHeight - (tileSize * mGameHeight));
mScreenMatrix.postTranslate(paddingLeft, paddingBottom);
mScreenMatrix.postScale(1f, -1f);
mScreenMatrix.postTranslate(0, mScreenHeight);
mScreenMatrixInverse = new Matrix();
mScreenMatrix.invert(mScreenMatrixInverse);
}
private static Matrix getTransformationMatrix(final int srcWidth,
final int srcHeight,
final int dstWidth,
final int dstHeight,
final boolean maintainAspectRatio) {
final Matrix matrix = new Matrix();
if (srcWidth != dstWidth || srcHeight != dstHeight) {
final float scaleFactorX = dstWidth / (float) srcWidth;
final float scaleFactorY = dstHeight / (float) srcHeight;
if (maintainAspectRatio) {
final float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
matrix.postScale(scaleFactor, scaleFactor);
} else {
matrix.postScale(scaleFactorX, scaleFactorY);
}
}
matrix.invert(new Matrix());
return matrix;
}
private float[] mapPointToImageSpace(float x, float y) {
float[] p = new float[2];
Matrix m = getImageViewMatrix();
Matrix m2 = new Matrix();
m.invert(m2);
p[0] = x;
p[1] = y;
m2.mapPoints(p);
return p;
}
/**
* Checks if a given point is within a rotated rectangle.
*
* @param point 2D point to check
* @param bound rectangle to rotate
* @param rot angle of rotation about rectangle center
* @return true if point is within rotated rectangle
*/
public static boolean pointInRotatedRect(float[] point, RectF bound, float rot) {
Matrix m = new Matrix();
float[] p = Arrays.copyOf(point, 2);
m.setRotate(rot, bound.centerX(), bound.centerY());
Matrix m0 = new Matrix();
if (!m.invert(m0))
return false;
m0.mapPoints(p);
return inclusiveContains(bound, p[0], p[1]);
}
private @Nullable Matrix findElementMatrix(@NonNull EditorElement element, @NonNull Matrix viewMatrix) {
Matrix inverse = findElementInverseMatrix(element, viewMatrix);
if (inverse != null) {
Matrix regular = new Matrix();
inverse.invert(regular);
return regular;
}
return null;
}
@Override
public void restoreDisplayMatrixFromData(Matrix matrix) {
if (matrix != null) {
Matrix invertDisplayMatrix = new Matrix();
matrix.invert(invertDisplayMatrix);
applyMatrixForData(invertDisplayMatrix);
getDisplayMatrix().set(new Matrix(matrix));
recalculateBoundingBox();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int width, int height) {
refreshCamera();
Matrix matrix = new Matrix();
matrix.postScale(width / 2000f, height / 2000f);
matrix.postTranslate(width / 2f, height / 2f);
matrix.invert(this.matrix);
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
Matrix matrix=new Matrix();
matrix.postTranslate(100, 200);
matrix.mapPoints(dst, src);
System.out.println("mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);//dst是 src通过矩阵变换的点 [0,0]点对应到 [100,200]
canvas.drawBitmap(bt, matrix, paint);
//通过下边的三个方法 可以通过 dst矩阵变化后的点 通过矩阵 逆向方法 转换到 invertDst 即是src原点位置 所以dst中[100,200]的点 即是inverDst[0,0]这个点即 src[0,0]点
Matrix invertMatrix = new Matrix();
matrix.invert(invertMatrix);
invertMatrix.mapPoints(invertDst, dst);
System.out.println("invertMatrix_____>>>mapPoints invertDst first Point X:" + invertDst[0] + "\t Y:" + invertDst[1]);
//测试postConcat
Matrix matrixCon =new Matrix();
matrixCon.postTranslate(100, 200);
matrix.postConcat(matrixCon);
matrix.mapPoints(dst, src);
System.out.println("matrixCon: mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("matrixCon: mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);
//测试Camera getMatrix
Camera camera=new Camera();
camera.save();
camera.translate(100,200,0);
camera.getMatrix(matrix);
camera.restore();
matrix.mapPoints(dst, src);
System.out.println("Camera: mapPoints_____>>>src first Point X:" + src[0] + "\t Y:" + src[1]);//src是记录的原点 [0,0]点
System.out.println("Camera: mapPoints_____>>>dst first Point X:"+ dst[0]+"\t Y:"+dst[1]);
}
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
txform.reset();
int viewWidth = getWidth();
int viewHeight = getHeight();
float viewCenterX = viewWidth / 2;
float viewCenterY = viewHeight / 2;
float scale;
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
scale = Math.max((float) (viewHeight + clipTop) / previewWidth, (float) (viewWidth + clipLeft) / previewHeight);
} else {
scale = Math.max((float) (viewHeight + clipTop) / previewHeight, (float) (viewWidth + clipLeft) / previewWidth);
}
float previewWidthScaled = previewWidth * scale;
float previewHeightScaled = previewHeight * scale;
float scaleX = previewHeightScaled / (viewWidth);
float scaleY = previewWidthScaled / (viewHeight);
txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
} else {
if (Surface.ROTATION_180 == rotation) {
txform.postRotate(180, viewCenterX, viewCenterY);
}
}
if (mirror) {
txform.postScale(-1, 1, viewCenterX, viewCenterY);
}
if (clipTop != 0 || clipLeft != 0) {
txform.postTranslate(-clipLeft / 2, -clipTop / 2);
}
textureView.setTransform(txform);
Matrix matrix = new Matrix();
matrix.postRotate(cameraSession.getDisplayOrientation());
matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
matrix.invert(this.matrix);
}
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
txform.reset();
int viewWidth = getWidth();
int viewHeight = getHeight();
float viewCenterX = viewWidth / 2;
float viewCenterY = viewHeight / 2;
float scale;
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
scale = Math.max((float) (viewHeight + clipTop + clipBottom) / previewWidth, (float) (viewWidth) / previewHeight);
} else {
scale = Math.max((float) (viewHeight + clipTop + clipBottom) / previewHeight, (float) (viewWidth) / previewWidth);
}
float previewWidthScaled = previewWidth * scale;
float previewHeightScaled = previewHeight * scale;
float scaleX = previewHeightScaled / (viewWidth);
float scaleY = previewWidthScaled / (viewHeight);
txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
} else {
if (Surface.ROTATION_180 == rotation) {
txform.postRotate(180, viewCenterX, viewCenterY);
}
}
if (mirror) {
txform.postScale(-1, 1, viewCenterX, viewCenterY);
}
if (clipTop != 0) {
txform.postTranslate(0, -clipTop / 2);
} else if (clipBottom != 0) {
txform.postTranslate(0, clipBottom / 2);
}
textureView.setTransform(txform);
Matrix matrix = new Matrix();
if (cameraSession != null) {
matrix.postRotate(cameraSession.getDisplayOrientation());
}
matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
matrix.invert(this.matrix);
}
public void setMatrix(Matrix m) {
invertMatrix = new Matrix();
m.invert(invertMatrix);
}
public static Bitmap processBitmap(Bitmap source,int size){
int image_height = source.getHeight();
int image_width = source.getWidth();
Bitmap croppedBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Matrix frameToCropTransformations = getTransformationMatrix(image_width,image_height,size,size,0,false);
Matrix cropToFrameTransformations = new Matrix();
frameToCropTransformations.invert(cropToFrameTransformations);
final Canvas canvas = new Canvas(croppedBitmap);
canvas.drawBitmap(source, frameToCropTransformations, null);
return croppedBitmap;
}
private Matrix inverse(Matrix source)
{
Matrix newMatrix = new Matrix();
source.invert(newMatrix);
return newMatrix;
}
private void adjustAspectRatio(int previewWidth, int previewHeight, int rotation) {
txform.reset();
int viewWidth = getWidth();
int viewHeight = getHeight();
float viewCenterX = viewWidth / 2;
float viewCenterY = viewHeight / 2;
float scale;
if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
scale = Math.max((float) (viewHeight + clipTop) / previewWidth, (float) (viewWidth + clipLeft) / previewHeight);
} else {
scale = Math.max((float) (viewHeight + clipTop) / previewHeight, (float) (viewWidth + clipLeft) / previewWidth);
}
float previewWidthScaled = previewWidth * scale;
float previewHeightScaled = previewHeight * scale;
float scaleX = previewHeightScaled / (viewWidth);
float scaleY = previewWidthScaled / (viewHeight);
txform.postScale(scaleX, scaleY, viewCenterX, viewCenterY);
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
txform.postRotate(90 * (rotation - 2), viewCenterX, viewCenterY);
} else {
if (Surface.ROTATION_180 == rotation) {
txform.postRotate(180, viewCenterX, viewCenterY);
}
}
if (mirror) {
txform.postScale(-1, 1, viewCenterX, viewCenterY);
}
if (clipTop != 0 || clipLeft != 0) {
txform.postTranslate(-clipLeft / 2, -clipTop / 2);
}
textureView.setTransform(txform);
Matrix matrix = new Matrix();
matrix.postRotate(cameraSession.getDisplayOrientation());
matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
matrix.invert(this.matrix);
}