Refine search
FixedSizeDrawable(State state, Drawable wrapped) { this.state = Preconditions.checkNotNull(state); this.wrapped = Preconditions.checkNotNull(wrapped); // We will do our own scaling. wrapped.setBounds(0, 0, wrapped.getIntrinsicWidth(), wrapped.getIntrinsicHeight()); matrix = new Matrix(); wrappedRect = new RectF(0, 0, wrapped.getIntrinsicWidth(), wrapped.getIntrinsicHeight()); bounds = new RectF(); }
public Matrix matrix() { if (matrix == null) { matrix = new Matrix(); matrix.postTranslate(-pivotX, -pivotY); matrix.postScale(scaleX, scaleY); matrix.postRotate(rotation, 0, 0); matrix.postTranslate(translateX + pivotX, translateY + pivotY); } return matrix; }
/** * Sets the scale factor to the specified values. x and y is pivot. * * @param scaleX * @param scaleY * @param x * @param y * @return */ public Matrix setZoom(float scaleX, float scaleY, float x, float y) { Matrix save = new Matrix(); save.set(mMatrixTouch); save.setScale(scaleX, scaleY, x, y); return save; }
public Matrix getValueToPixelMatrix() { mMBuffer1.set(mMatrixValueToPx); mMBuffer1.postConcat(mViewPortHandler.mMatrixTouch); mMBuffer1.postConcat(mMatrixOffset); return mMBuffer1; }
/** 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); } }
/** * Rotate and/or flip the image to match the given exif orientation. * * @param pool A pool that may or may not contain an image of the necessary * dimensions. * @param inBitmap The bitmap to rotate/flip. * @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(@NonNull BitmapPool pool, @NonNull Bitmap inBitmap, int exifOrientation) { if (!isExifOrientationRequired(exifOrientation)) { return inBitmap; } final Matrix matrix = new Matrix(); initializeMatrixForRotation(exifOrientation, matrix); // From Bitmap.createBitmap. final RectF newRect = new RectF(0, 0, inBitmap.getWidth(), inBitmap.getHeight()); matrix.mapRect(newRect); final int newWidth = Math.round(newRect.width()); final int newHeight = Math.round(newRect.height()); Bitmap.Config config = getNonNullConfig(inBitmap); Bitmap result = pool.get(newWidth, newHeight, config); matrix.postTranslate(-newRect.left, -newRect.top); result.setHasAlpha(inBitmap.hasAlpha()); applyMatrix(inBitmap, result, matrix); return result; }
@Test public void testMapRect() { final Matrix matrix = new Matrix(); matrix.postScale(2.0f, 3.0f); final RectF input = new RectF(1.0f, 1.0f, 2.0f, 2.0f); final RectF output1 = new RectF(); matrix.mapRect(output1, input); assertThat(output1) .isEqualTo(new RectF(2.0f, 3.0f, 4.0f, 6.0f)); matrix.postScale(-1.0f, -1.0f); final RectF output2 = new RectF(); matrix.mapRect(output2, input); assertThat(output2) .isEqualTo(new RectF(-4.0f, -6.0f, -2.0f, -3.0f)); }
@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(); }
private void updateShaderMatrix() { float scale; float dx = 0; float dy = 0; mShaderMatrix.set(null); if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) { scale = mDrawableRect.height() / (float) mBitmapHeight; dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f; } else { scale = mDrawableRect.width() / (float) mBitmapWidth; dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f; } mShaderMatrix.setScale(scale, scale); mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top); mBitmapShader.setLocalMatrix(mShaderMatrix); }
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(); }
@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)); }
@Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin); // Resize the original bitmap to fit the new bound Matrix shaderMatrix = new Matrix(); shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL); bitmapShader.setLocalMatrix(shaderMatrix); }
@Override public void onDraw(Canvas canvas, Paint paint, Bitmap buffer) { if (mCornerRadius == 0) { canvas.drawBitmap(buffer, null, mDstRectF, paint); return; } if (mShader == null) { mShader = new BitmapShader(buffer, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); final Matrix shaderMatrix = new Matrix(); shaderMatrix.setTranslate(mDstRectF.left, mDstRectF.top); shaderMatrix.preScale(mDstRectF.width() / buffer.getWidth(), mDstRectF.height() / buffer.getHeight()); mShader.setLocalMatrix(shaderMatrix); } paint.setShader(mShader); canvas.drawRoundRect(mDstRectF, mCornerRadius, mCornerRadius, paint); } }
/** * Post-scales by the specified scale factors. * * @param scaleX * @param scaleY * @return */ public Matrix zoom(float scaleX, float scaleY) { Matrix save = new Matrix(); zoom(scaleX, scaleY, save); return save; }
@Test public void testMapPoints() { final Matrix matrix = new Matrix(); matrix.postTranslate(-1.0f, -2.0f); matrix.postScale(2.0f, 3.0f); final float[] input = { 0.0f, 0.0f, 1.0f, 2.0f }; final float[] output = new float[input.length]; matrix.mapPoints(output, input); assertThat(output) .usingExactEquality() .containsExactly(-2.0f, -6.0f, 0.0f, 0.0f); }
@Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); RadialGradient vignette = new RadialGradient( mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f, new int[]{0, 0, 0x7f000000}, new float[]{0.0f, 0.7f, 1.0f}, Shader.TileMode.CLAMP); Matrix oval = new Matrix(); oval.setScale(1.0f, 0.7f); vignette.setLocalMatrix(oval); paint.setShader(new ComposeShader(bitmapShader, vignette, PorterDuff.Mode.SRC_OVER)); } }
public static List<Path> transformScale(float ratioWidth, float ratioHeight, List<Path> originPaths, List<String> orginSvgs) { Matrix matrix = new Matrix(); matrix.setScale(ratioWidth, ratioHeight); List<Path> paths = new ArrayList<>(); if (Build.VERSION.SDK_INT > 16) { for (Path path : originPaths) { Path nPath = new Path(); path.transform(matrix, nPath); paths.add(nPath); } } else { for (String svgPath : orginSvgs) { Path path = new Path(); PathDataNode[] nodes = createNodesFromPathData(svgPath); transformScaleNodes(ratioWidth, ratioHeight, nodes); PathDataNode.nodesToPath(nodes, path); paths.add(path); } } return paths; }