下面列出了android.graphics.Matrix#isIdentity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
@Override
public void onReady(@NonNull Renderer renderer, @Nullable Matrix cropMatrix, @Nullable Point size) {
if (cropMatrix != null && size != null && isRendererOfMainImage(renderer)) {
boolean changedBefore = isChanged();
Matrix imageCropMatrix = editorElementHierarchy.getImageCrop().getLocalMatrix();
this.size.set(size.x, size.y);
if (imageCropMatrix.isIdentity()) {
imageCropMatrix.set(cropMatrix);
if (circleEditing) {
Matrix userCropMatrix = editorElementHierarchy.getCropEditorElement().getLocalMatrix();
if (size.x > size.y) {
userCropMatrix.setScale(size.y / (float) size.x, 1f);
} else {
userCropMatrix.setScale(1f, size.x / (float) size.y);
}
}
editorElementHierarchy.doneCrop(visibleViewPort, null);
if (!changedBefore) {
undoRedoStacks.clear(editorElementHierarchy.getRoot());
}
if (circleEditing) {
startCrop();
}
}
}
}
/**
* Rotate and/or flip the image to match the given exif orientation.
*
* @param toOrient The bitmap to rotate/flip.
* @param pool A pool that may or may not contain an image of the necessary dimensions.
* @param exifOrientation the exif orientation [1-8].
* @return The rotated and/or flipped image or toOrient if no rotation or flip was necessary.
*/
public static Bitmap rotateImageExif(Bitmap toOrient, BitmapPool pool, int exifOrientation) {
final Matrix matrix = new Matrix();
initializeMatrixForRotation(exifOrientation, matrix);
if (matrix.isIdentity()) {
return toOrient;
}
// From Bitmap.createBitmap.
final RectF newRect = new RectF(0, 0, toOrient.getWidth(), toOrient.getHeight());
matrix.mapRect(newRect);
final int newWidth = Math.round(newRect.width());
final int newHeight = Math.round(newRect.height());
Bitmap.Config config = getSafeConfig(toOrient);
Bitmap result = pool.get(newWidth, newHeight, config);
if (result == null) {
result = Bitmap.createBitmap(newWidth, newHeight, config);
}
matrix.postTranslate(-newRect.left, -newRect.top);
final Canvas canvas = new Canvas(result);
final Paint paint = new Paint(PAINT_FLAGS);
canvas.drawBitmap(toOrient, matrix, paint);
return result;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
private void transformPointToViewLocal(float[] point, View child) {
point[0] += mParent.getScrollX() - child.getLeft();
point[1] += mParent.getScrollY() - child.getTop();
Matrix matrix = child.getMatrix();
if (!matrix.isIdentity()) {
Matrix invert = getTempMatrix();
boolean result = matrix.invert(invert);
if (result) {
invert.mapPoints(point);
}
}
}
@Override
public void setImageMatrix( Matrix matrix ) {
Matrix current = getImageMatrix();
boolean needUpdate = false;
if ( matrix == null && !current.isIdentity() || matrix != null && !current.equals( matrix ) ) {
needUpdate = true;
}
super.setImageMatrix( matrix );
if ( needUpdate ) onImageMatrixChanged();
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void setImageMatrix(Matrix matrix) {
// collaps null and identity to just null
if (matrix != null && matrix.isIdentity()) {
matrix = null;
}
// don't invalidate unless we're actually changing our matrix
if (matrix == null && !mMatrix.isIdentity() ||
matrix != null && !mMatrix.equals(matrix)) {
mMatrix.set(matrix);
configureBounds();
invalidate();
}
}
public void setImageMatrix(Matrix m){
if (m != null && m.isIdentity()) {
m = null;
}
// don't invalidate unless we're actually changing our matrix
if (m == null && !this.mMatrix.isIdentity() || m != null && !this.mMatrix.equals(m)) {
this.mMatrix.set(m);
invalidate();
}
}
public void setImageMatrix(Matrix m) {
if (m != null && m.isIdentity()) {
m = null;
}
// don't invalidate unless we're actually changing our matrix
if (m == null && !this.mMatrix.isIdentity() || m != null && !this.mMatrix.equals(m)) {
this.mMatrix.set(m);
invalidate();
}
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
public void getHitRect(@NonNull Rect outRect) {
Matrix matrix = getMatrix();
if (matrix.isIdentity()) {
outRect.set(getLeft(), getTop(), getRight(), getBottom());
} else {
tmpHitRect.set(0, 0, getWidth(), getHeight());
matrix.mapRect(tmpHitRect);
outRect.set((int) tmpHitRect.left + getLeft(), (int) tmpHitRect.top + getTop(),
(int) tmpHitRect.right + getLeft(), (int) tmpHitRect.bottom + getTop());
}
outRect.left -= touchMargin.left;
outRect.top -= touchMargin.top;
outRect.right += touchMargin.right;
outRect.bottom += touchMargin.bottom;
}
@Override
@NonNull
protected BitmapWorkerResult doInBackground(Void... params) {
if (mInputUri == null) {
return new BitmapWorkerResult(new NullPointerException("Input Uri cannot be null"));
}
final ParcelFileDescriptor parcelFileDescriptor;
try {
parcelFileDescriptor = mContext.getContentResolver().openFileDescriptor(mInputUri, "r");
} catch (FileNotFoundException e) {
return new BitmapWorkerResult(e);
}
final FileDescriptor fileDescriptor;
if (parcelFileDescriptor != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
} else {
return new BitmapWorkerResult(new NullPointerException("ParcelFileDescriptor was null for given Uri: [" + mInputUri + "]"));
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
if (options.outWidth == -1 || options.outHeight == -1) {
return new BitmapWorkerResult(new IllegalArgumentException("Bounds for bitmap could not be retrieved from the Uri: [" + mInputUri + "]"));
}
options.inSampleSize = BitmapLoadUtils.calculateInSampleSize(options, mRequiredWidth, mRequiredHeight);
options.inJustDecodeBounds = false;
Bitmap decodeSampledBitmap = null;
boolean decodeAttemptSuccess = false;
while (!decodeAttemptSuccess) {
try {
decodeSampledBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor, null, options);
decodeAttemptSuccess = true;
} catch (OutOfMemoryError error) {
Log.e(TAG, "doInBackground: BitmapFactory.decodeFileDescriptor: ", error);
options.inSampleSize *= 2;
}
}
if (decodeSampledBitmap == null) {
return new BitmapWorkerResult(new IllegalArgumentException("Bitmap could not be decoded from the Uri: [" + mInputUri + "]"));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
BitmapLoadUtils.close(parcelFileDescriptor);
}
int exifOrientation = BitmapLoadUtils.getExifOrientation(mContext, mInputUri);
int exifDegrees = BitmapLoadUtils.exifToDegrees(exifOrientation);
int exifTranslation = BitmapLoadUtils.exifToTranslation(exifOrientation);
ExifInfo exifInfo = new ExifInfo(exifOrientation, exifDegrees, exifTranslation);
Matrix matrix = new Matrix();
if (exifDegrees != 0) {
matrix.preRotate(exifDegrees);
}
if (exifTranslation != 1) {
matrix.postScale(exifTranslation, 1);
}
if (!matrix.isIdentity()) {
return new BitmapWorkerResult(BitmapLoadUtils.transformBitmap(decodeSampledBitmap, matrix), exifInfo);
}
return new BitmapWorkerResult(decodeSampledBitmap, exifInfo);
}