private Bitmap rotatingImage(Bitmap bitmap, int angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); }
public void setRotationBy(float degrees) { mSuppMatrix.postRotate(degrees % 360); checkAndDisplayMatrix(); }
private void applyAnima() { mAnimaMatrix.reset(); mAnimaMatrix.postTranslate(-mBaseRect.left, -mBaseRect.top); mAnimaMatrix.postTranslate(mRotateCenter.x, mRotateCenter.y); mAnimaMatrix.postTranslate(-mHalfBaseRectWidth, -mHalfBaseRectHeight); mAnimaMatrix.postRotate(mDegrees, mRotateCenter.x, mRotateCenter.y); mAnimaMatrix.postScale(mScale, mScale, mScaleCenter.x, mScaleCenter.y); mAnimaMatrix.postTranslate(mTranslateX, mTranslateY); executeTranslate(); }
private void applyAnima() { mAnimaMatrix.reset(); mAnimaMatrix.postTranslate(-mBaseRect.left, -mBaseRect.top); mAnimaMatrix.postTranslate(mRotateCenter.x, mRotateCenter.y); mAnimaMatrix.postTranslate(-mHalfBaseRectWidth, -mHalfBaseRectHeight); mAnimaMatrix.postRotate(mDegrees, mRotateCenter.x, mRotateCenter.y); mAnimaMatrix.postScale(mScale, mScale, mScaleCenter.x, mScaleCenter.y); mAnimaMatrix.postTranslate(mTranslateX, mTranslateY); executeTranslate(); }
public void rotate(float degrees) { mDegrees += degrees; int centerX = (int) (mWidgetRect.left + mWidgetRect.width() / 2); int centerY = (int) (mWidgetRect.top + mWidgetRect.height() /2); mAnimaMatrix.postRotate(degrees, centerX, centerY); executeTranslate(); } }
private void setMatrix() { mMatrix.reset(); mMatrix.setTranslate(mCenter.x - mImgWidth * 0.5f, mCenter.y - mImgHeight * 0.5f); mMatrix.postScale(mScale, mScale, mCenter.x, mCenter.y); mMatrix.postRotate(mAngle, mCenter.x, mCenter.y); }
@Override @NonNull public Path getPath(float startX, float startY, float endX, float endY) { double dx = endX - startX; double dy = endY - startY; float length = (float) Math.hypot(dx, dy); double angle = Math.atan2(dy, dx); mTempMatrix.setScale(length, length); mTempMatrix.postRotate((float) Math.toDegrees(angle)); mTempMatrix.postTranslate(startX, startY); Path path = new Path(); mPatternPath.transform(mTempMatrix, path); return path; }
private void updateMatrixScaleRotate() { mTransformMatrix.reset(); mTransformMatrix.setScale(mContentScaleX * mContentScaleMultiplier, mContentScaleY * mContentScaleMultiplier, mPivotPointX, mPivotPointY); mTransformMatrix.postRotate(mContentRotation, mPivotPointX, mPivotPointY); setTransform(mTransformMatrix); }
public Matrix getRotateMatrix() { // By default this is an identity matrix Matrix matrix = new Matrix(); if (bitmap != null && rotation != 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 = bitmap.getWidth() / 2; int cy = bitmap.getHeight() / 2; matrix.preTranslate(-cx, -cy); matrix.postRotate(rotation); matrix.postTranslate(getWidth() / 2, getHeight() / 2); } return matrix; }
/** Applies a texture rotation to a {@link TextureView}. */ private static void applyTextureViewRotation(TextureView textureView, int textureViewRotation) { float textureViewWidth = textureView.getWidth(); float textureViewHeight = textureView.getHeight(); if (textureViewWidth == 0 || textureViewHeight == 0 || textureViewRotation == 0) { textureView.setTransform(null); } else { Matrix transformMatrix = new Matrix(); float pivotX = textureViewWidth / 2; float pivotY = textureViewHeight / 2; transformMatrix.postRotate(textureViewRotation, pivotX, pivotY); // After rotation, scale the rotated texture to fit the TextureView size. RectF originalTextureRect = new RectF(0, 0, textureViewWidth, textureViewHeight); RectF rotatedTextureRect = new RectF(); transformMatrix.mapRect(rotatedTextureRect, originalTextureRect); transformMatrix.postScale( textureViewWidth / rotatedTextureRect.width(), textureViewHeight / rotatedTextureRect.height(), pivotX, pivotY); textureView.setTransform(transformMatrix); } }
@Override protected synchronized void onDraw(Canvas canvas) { super.onDraw(canvas); if (drawLoading) { canvas.save(); degree = ((int) (degree + 3.0F)); degree %= 360; matrix.reset(); matrix.postRotate(degree, loading.getWidth() / 2, loading.getHeight() / 2); canvas.translate(getPaddingLeft() + getThumb().getBounds().left + drawable.getIntrinsicWidth() / 2 - loading.getWidth() / 2 - getThumbOffset(), getPaddingTop() + getThumb().getBounds().top + drawable.getIntrinsicHeight() / 2 - loading.getHeight() / 2); canvas.drawBitmap(loading, matrix, null); canvas.restore(); invalidate(); } } }
/** * This method rotates current image. * * @param deltaAngle - rotation angle * @param px - rotation center X * @param py - rotation center Y */ public void postRotate(float deltaAngle, float px, float py) { if (deltaAngle != 0) { mCurrentImageMatrix.postRotate(deltaAngle, px, py); setImageMatrix(mCurrentImageMatrix); if (mTransformImageListener != null) { mTransformImageListener.onRotate(getMatrixAngle(mCurrentImageMatrix)); } } }
@Test public void testReset() { final Matrix matrix = new Matrix(); matrix.postScale(2.0f, 2.0f); matrix.postTranslate(1.0f, 2.0f); matrix.postRotate(45.0f); matrix.reset(); assertThat(matrix.isIdentity()) .isTrue(); }
@Test public void testSet() { final Matrix matrix1 = new Matrix(); matrix1.postScale(2.0f, 2.0f); matrix1.postTranslate(1.0f, 2.0f); matrix1.postRotate(45.0f); final Matrix matrix2 = new Matrix(); matrix2.set(matrix1); assertThat(matrix1) .isEqualTo(matrix2); matrix2.set(null); assertThat(matrix2.isIdentity()) .isTrue(); }
@Test public void testPostRotate() { final Matrix matrix = new Matrix(); matrix.postRotate(90.0f); assertPointsEqual(mapPoint(matrix, 0.0f, 1.0f), new PointF(-1.0f, 0.0f)); matrix.postRotate(90.0f); assertPointsEqual(mapPoint(matrix, 0.0f, 1.0f), new PointF(0.0f, -1.0f)); matrix.postRotate(90.0f); assertPointsEqual(mapPoint(matrix, 0.0f, 1.0f), new PointF(1.0f, 0.0f)); matrix.postRotate(90.0f); assertPointsEqual(mapPoint(matrix, 0.0f, 1.0f), new PointF(0.0f, 1.0f)); matrix.setTranslate(1.0f, 2.0f); matrix.postRotate(45.0f, 0.0f, 1.0f); assertPointsEqual(mapPoint(matrix, 0.0f, 1.0f), new PointF(-0.70710677f, 3.1213202f)); }
private void updateShaderMatrix() { if (!clipRect.isEmpty() && bitmapPaint.getShader() != null) { getController().getState().get(tmpMatrix); // Including paddings & reverting rotation (will be applied later in draw() method) tmpMatrix.postTranslate(getPaddingLeft(), getPaddingTop()); tmpMatrix.postRotate(-clipRotation, clipRect.centerX(), clipRect.centerY()); bitmapPaint.getShader().setLocalMatrix(tmpMatrix); } }
@Test public void testSetConcat() { final Matrix scaleMatrix = new Matrix(); scaleMatrix.setScale(2.0f, 3.0f); final Matrix translateMatrix = new Matrix(); translateMatrix.postTranslate(5.0f, 7.0f); final Matrix matrix = new Matrix(); matrix.setConcat(translateMatrix, scaleMatrix); assertPointsEqual(mapPoint(matrix, 2.0f, 2.0f), new PointF(9.0f, 13.0f)); final Matrix rotateMatrix = new Matrix(); rotateMatrix.postRotate(90.0f); matrix.setConcat(rotateMatrix, matrix); assertPointsEqual(mapPoint(matrix, 2.0f, 2.0f), new PointF(-13.0f, 9.0f)); }
@Test public void testPostConcat() { final Matrix matrix = new Matrix(); matrix.postScale(2.0f, 3.0f); final Matrix translateMatrix = new Matrix(); translateMatrix.postTranslate(5.0f, 7.0f); matrix.postConcat(translateMatrix); assertPointsEqual(mapPoint(matrix, 2.0f, 2.0f), new PointF(9.0f, 13.0f)); final Matrix rotateMatrix = new Matrix(); rotateMatrix.postRotate(90.0f); matrix.postConcat(rotateMatrix); assertPointsEqual(mapPoint(matrix, 2.0f, 2.0f), new PointF(-13.0f, 9.0f)); }
@Test public void postOperationsAreQueued() { Matrix m = new Matrix(); m.postRotate(4, 8, 15); m.postTranslate(16, 23); m.postSkew(42, 108); assertThat(shadowOf(m).getPostOperations()).containsExactly( "rotate 4.0 8.0 15.0", "translate 16.0 23.0", "skew 42.0 108.0" ); }
@Test public void testRectStaysRect() { final Matrix matrix = new Matrix(); assertThat(matrix.rectStaysRect()) .isTrue(); matrix.postScale(2.0f, 2.0f); assertThat(matrix.rectStaysRect()) .isTrue(); matrix.postTranslate(1.0f, 2.0f); assertThat(matrix.rectStaysRect()) .isTrue(); matrix.postRotate(45.0f); assertThat(matrix.rectStaysRect()) .isFalse(); matrix.postRotate(45.0f); assertThat(matrix.rectStaysRect()) .isTrue(); }