/** * This method translates current image. * * @param deltaX - horizontal shift * @param deltaY - vertical shift */ public void postTranslate(float deltaX, float deltaY) { if (deltaX != 0 || deltaY != 0) { mCurrentImageMatrix.postTranslate(deltaX, deltaY); setImageMatrix(mCurrentImageMatrix); } }
/** * Post-translates to the specified points. Output matrix allows for caching objects. * * @param transformedPts * @return */ public void translate(final float[] transformedPts, Matrix outputMatrix) { outputMatrix.reset(); outputMatrix.set(mMatrixTouch); final float x = transformedPts[0] - offsetLeft(); final float y = transformedPts[1] - offsetTop(); outputMatrix.postTranslate(-x, -y); }
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); }
final Matrix matrix = t.getMatrix(); camera.save(); camera.translate(0, 0, Math.abs(degrees)*2); camera.getMatrix(matrix); camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY);
private void updateMatrix(){ if(mBitmap == null) return; Rect bounds = getBounds(); if(bounds.width() == 0 || bounds.height() == 0) return; mMatrix.reset(); float scale = bounds.height() / (float)Math.min(mBitmap.getWidth(), mBitmap.getHeight()); mMatrix.setScale(scale, scale, 0, 0); mMatrix.postTranslate((bounds.height() - mBitmap.getWidth() * scale) / 2, (bounds.height() - mBitmap.getHeight() * scale) / 2); mBitmapShader.setLocalMatrix(mMatrix); }
/** * Prepares the matrix that transforms values to pixels. Calculates the * scale factors from the charts size and offsets. * * @param xChartMin * @param deltaX * @param deltaY * @param yChartMin */ public void prepareMatrixValuePx(float xChartMin, float deltaX, float deltaY, float yChartMin) { float scaleX = (float) ((mViewPortHandler.contentWidth()) / deltaX); float scaleY = (float) ((mViewPortHandler.contentHeight()) / deltaY); if (Float.isInfinite(scaleX)) { scaleX = 0; } if (Float.isInfinite(scaleY)) { scaleY = 0; } // setup all matrices mMatrixValueToPx.reset(); mMatrixValueToPx.postTranslate(-xChartMin, -yChartMin); mMatrixValueToPx.postScale(scaleX, -scaleY); }
private void updateMatrix(){ if(mBitmap == null) return; Rect bounds = getBounds(); if(bounds.width() == 0 || bounds.height() == 0) return; mMatrix.reset(); float scale = bounds.height() / (float)Math.min(mBitmap.getWidth(), mBitmap.getHeight()); mMatrix.setScale(scale, scale, 0, 0); mMatrix.postTranslate(bounds.exactCenterX() - mBitmap.getWidth() * scale / 2, bounds.exactCenterY() - mBitmap.getHeight() * scale / 2); mBitmapShader.setLocalMatrix(mMatrix); }
/** * Centers the viewport around the specified position (x-index and y-value) * in the chart. Centering the viewport outside the bounds of the chart is * not possible. Makes most sense in combination with the * setScaleMinima(...) method. * * @param transformedPts the position to center view viewport to * @param view * @return save */ public void centerViewPort(final float[] transformedPts, final View view) { Matrix save = mCenterViewPortMatrixBuffer; save.reset(); save.set(mMatrixTouch); final float x = transformedPts[0] - offsetLeft(); final float y = transformedPts[1] - offsetTop(); save.postTranslate(-x, -y); refresh(save, view, true); }
/** * Performs all necessary operations needed for dragging. * * @param event */ private void performDrag(MotionEvent event, float distanceX, float distanceY) { mLastGesture = ChartGesture.DRAG; mMatrix.set(mSavedMatrix); OnChartGestureListener l = mChart.getOnChartGestureListener(); // check if axis is inverted if (inverted()) { // if there is an inverted horizontalbarchart if (mChart instanceof HorizontalBarChart) { distanceX = -distanceX; } else { distanceY = -distanceY; } } mMatrix.postTranslate(distanceX, distanceY); if (l != null) l.onChartTranslate(event, distanceX, distanceY); }
/** * Prepares the matrix that contains all offsets. * * @param inverted */ public void prepareMatrixOffset(boolean inverted) { mMatrixOffset.reset(); // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); if (!inverted) mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); else { mMatrixOffset .setTranslate(mViewPortHandler.offsetLeft(), -mViewPortHandler.offsetTop()); mMatrixOffset.postScale(1.0f, -1.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 testPostTranslate() { final Matrix matrix1 = new Matrix(); matrix1.postTranslate(1.0f, 1.0f); assertPointsEqual(mapPoint(matrix1, 1.0f, 1.0f), new PointF(2.0f, 2.0f)); matrix1.postTranslate(2.0f, 2.0f); assertPointsEqual(mapPoint(matrix1, 1.0f, 1.0f), new PointF(4.0f, 4.0f)); final Matrix matrix2 = new Matrix(); matrix2.setScale(2.0f, 2.0f); matrix2.postTranslate(-5.0f, 10.0f); assertPointsEqual(mapPoint(matrix2, 1.0f, 1.0f), new PointF(-3.0f, 12.0f)); }
@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(); }
/** * Prepares the matrix that contains all offsets. * * @param inverted */ public void prepareMatrixOffset(boolean inverted) { mMatrixOffset.reset(); // offset.postTranslate(mOffsetLeft, getHeight() - mOffsetBottom); if (!inverted) mMatrixOffset.postTranslate(mViewPortHandler.offsetLeft(), mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); else { mMatrixOffset .setTranslate( -(mViewPortHandler.getChartWidth() - mViewPortHandler.offsetRight()), mViewPortHandler.getChartHeight() - mViewPortHandler.offsetBottom()); mMatrixOffset.postScale(-1.0f, 1.0f); } // mMatrixOffset.set(offset); // mMatrixOffset.reset(); // // mMatrixOffset.postTranslate(mOffsetLeft, getHeight() - // mOffsetBottom); } }
@Override public void run() { if (mScroller.isFinished()) { return; // remaining post that should not be handled } if (mScroller.computeScrollOffset()) { final int newX = mScroller.getCurrX(); final int newY = mScroller.getCurrY(); mSuppMatrix.postTranslate(mCurrentX - newX, mCurrentY - newY); checkAndDisplayMatrix(); mCurrentX = newX; mCurrentY = newY; // Post On animation Compat.postOnAnimation(mImageView, this); } } }
@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 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); }
@Test public void testMapVectors() { 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.mapVectors(output, input); assertThat(output) .usingExactEquality() .containsExactly(0.0f, 0.0f, 2.0f, 6.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" ); }