Java Code Examples for android.graphics.Matrix.postTranslate()

The following are Jave code examples for showing how to use postTranslate() of the android.graphics.Matrix class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: GitHub   File: ScalingUtils.java   Source Code and License Vote up 7 votes
@Override
public void getTransformImpl(
    Matrix outTransform,
    Rect parentRect,
    int childWidth,
    int childHeight,
    float focusX,
    float focusY,
    float scaleX,
    float scaleY) {
  float scale = Math.min(scaleX, scaleY);
  float dx = parentRect.left;
  float dy = parentRect.top + (parentRect.height() - childHeight * scale);
  outTransform.setScale(scale, scale);
  outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
 
Example 2
Project: Zoomable   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
/**
 * Calculates the zoom transformation that would zoom to the desired scale and position the image
 * so that the given image point corresponds to the given view point.
 *
 * @param outTransform the matrix to store the result to
 * @param scale desired scale, will be limited to {min, max} scale factor
 * @param imagePoint 2D point in image's relative coordinate system (i.e. 0 <= x, y <= 1)
 * @param viewPoint 2D point in view's absolute coordinate system
 * @param limitFlags whether to limit translation and/or scale.
 * @return whether or not the transform has been corrected due to limitation
 */
protected boolean calculateZoomToPointTransform(
    Matrix outTransform,
    float scale,
    PointF imagePoint,
    PointF viewPoint,
    @LimitFlag int limitFlags) {
  float[] viewAbsolute = mTempValues;
  viewAbsolute[0] = imagePoint.x;
  viewAbsolute[1] = imagePoint.y;
  mapRelativeToAbsolute(viewAbsolute, viewAbsolute, 1);
  float distanceX = viewPoint.x - viewAbsolute[0];
  float distanceY = viewPoint.y - viewAbsolute[1];
  boolean transformCorrected = false;
  outTransform.setScale(scale, scale, viewAbsolute[0], viewAbsolute[1]);
  transformCorrected |= limitScale(outTransform, viewAbsolute[0], viewAbsolute[1], limitFlags);
  outTransform.postTranslate(distanceX, distanceY);
  transformCorrected |= limitTranslation(outTransform, limitFlags);
  return transformCorrected;
}
 
Example 3
Project: android-image-classification   File: MainActivity.java   Source Code and License Vote up 6 votes
private void renderDebug(final Canvas canvas) {
    if (!debug) {
        return;
    }
    final Bitmap copy = cropCopyBitmap;
    if (copy != null) {
        final Matrix matrix = new Matrix();
        final float scaleFactor = 2;
        matrix.postScale(scaleFactor, scaleFactor);
        matrix.postTranslate(
                canvas.getWidth() - copy.getWidth() * scaleFactor,
                canvas.getHeight() - copy.getHeight() * scaleFactor);
        canvas.drawBitmap(copy, matrix, new Paint());

        final Vector<String> lines = new Vector<>();

        lines.add("Frame: " + previewWidth + "x" + previewHeight);
        lines.add("Crop: " + copy.getWidth() + "x" + copy.getHeight());
        lines.add("View: " + canvas.getWidth() + "x" + canvas.getHeight());
        lines.add("Inference time: " + lasProcessingTimeMs + "ms");

        borderedText.drawLines(canvas, 10, canvas.getHeight() - 10, lines);
    }
}
 
Example 4
Project: react-native-camera-android-simple   File: ImageUtils.java   Source Code and License Vote up 6 votes
public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) {
    boolean reverseWidthHeight = ((rotation / 90) % 2) == 1;

    int width = reverseWidthHeight ? bitmap.getHeight() : bitmap.getWidth();
    int height = reverseWidthHeight ? bitmap.getWidth() : bitmap.getHeight();

    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    Bitmap newBitmap = Bitmap.createBitmap(width, height, conf);
    Canvas canvas = new Canvas(newBitmap);

    Matrix matrix = new Matrix();
    matrix.setRotate(rotation, bitmap.getWidth() / 2f, bitmap.getHeight() / 2f);
    matrix.postTranslate(
            canvas.getWidth() / 2f - bitmap.getWidth() / 2f,
            canvas.getHeight() / 2f - bitmap.getHeight() / 2f
    );

    canvas.drawBitmap(bitmap, matrix, new Paint());

    return newBitmap;
}
 
Example 5
Project: GitHub   File: ScalingUtils.java   Source Code and License Vote up 6 votes
@Override
public void getTransformImpl(
    Matrix outTransform,
    Rect parentRect,
    int childWidth,
    int childHeight,
    float focusX,
    float focusY,
    float scaleX,
    float scaleY) {
  float scale = Math.min(scaleX, scaleY);
  float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
  float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
  outTransform.setScale(scale, scale);
  outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
 
Example 6
Project: CommentGallery   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
protected void calculateTranslateTransform(Matrix outTransform, float distanceX, float distanceY) {
    outTransform.postTranslate(distanceX, distanceY);
    float[] viewAbsolute = mTempValues;
    viewAbsolute[0] = 0.5f;
    viewAbsolute[1] = 0.5f;
    mapRelativeToAbsolute(viewAbsolute, viewAbsolute, 1);
    float scale = (getViewBounds().height() - distanceY) / getViewBounds().height();
    outTransform.postScale(scale, scale, viewAbsolute[0], viewAbsolute[1]);
    limitScale(outTransform, viewAbsolute[0], viewAbsolute[1], LIMIT_ALL);
}
 
Example 7
Project: GitHub   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
/**
 * Limits the translation so that there are no empty spaces on the sides if possible.
 *
 * <p> The image is attempted to be centered within the view bounds if the transformed image is
 * smaller. There will be no empty spaces within the view bounds if the transformed image is
 * bigger. This applies to each dimension (horizontal and vertical) independently.
 *
 * @param limitTypes whether to limit translation along the specific axis.
 * @return whether limiting has been applied or not
 */
private boolean limitTranslation(Matrix transform, @LimitFlag int limitTypes) {
  if (!shouldLimit(limitTypes, LIMIT_TRANSLATION_X | LIMIT_TRANSLATION_Y)) {
    return false;
  }
  RectF b = mTempRect;
  b.set(mImageBounds);
  transform.mapRect(b);
  float offsetLeft = shouldLimit(limitTypes, LIMIT_TRANSLATION_X) ?
      getOffset(b.left, b.right, mViewBounds.left, mViewBounds.right, mImageBounds.centerX()) : 0;
  float offsetTop = shouldLimit(limitTypes, LIMIT_TRANSLATION_Y) ?
      getOffset(b.top, b.bottom, mViewBounds.top, mViewBounds.bottom, mImageBounds.centerY()) : 0;
  if (offsetLeft != 0 || offsetTop != 0) {
    transform.postTranslate(offsetLeft, offsetTop);
    return true;
  }
  return false;
}
 
Example 8
Project: AppRTC-Android   File: TextureBufferImpl.java   Source Code and License Vote up 6 votes
@Override
public VideoFrame.Buffer cropAndScale(
    int cropX, int cropY, int cropWidth, int cropHeight, int scaleWidth, int scaleHeight) {
  retain();
  Matrix newMatrix = new Matrix(transformMatrix);
  newMatrix.postScale(cropWidth / (float) width, cropHeight / (float) height);
  newMatrix.postTranslate(cropX / (float) width, cropY / (float) height);

  return new TextureBufferImpl(
      scaleWidth, scaleHeight, type, id, newMatrix, surfaceTextureHelper, new Runnable() {
        @Override
        public void run() {
          release();
        }
      });
}
 
Example 9
Project: GitHub   File: ViewPortHandler.java   Source Code and License Vote up 5 votes
/**
 * 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);
}
 
Example 10
Project: android-project-gallery   File: AnimatorProxy.java   Source Code and License Vote up 5 votes
private void transformMatrix(Matrix m, View view) {
    final float w = view.getWidth();
    final float h = view.getHeight();
    final boolean hasPivot = mHasPivot;
    final float pX = hasPivot ? mPivotX : w / 2f;
    final float pY = hasPivot ? mPivotY : h / 2f;

    final float rX = mRotationX;
    final float rY = mRotationY;
    final float rZ = mRotationZ;
    if ((rX != 0) || (rY != 0) || (rZ != 0)) {
        final Camera camera = mCamera;
        camera.save();
        camera.rotateX(rX);
        camera.rotateY(rY);
        camera.rotateZ(-rZ);
        camera.getMatrix(m);
        camera.restore();
        m.preTranslate(-pX, -pY);
        m.postTranslate(pX, pY);
    }

    final float sX = mScaleX;
    final float sY = mScaleY;
    if ((sX != 1.0f) || (sY != 1.0f)) {
        m.postScale(sX, sY);
        final float sPX = -(pX / w) * ((sX * w) - w);
        final float sPY = -(pY / h) * ((sY * h) - h);
        m.postTranslate(sPX, sPY);
    }

    m.postTranslate(mTranslationX, mTranslationY);
}
 
Example 11
Project: GitHub   File: ImageCropActivity.java   Source Code and License Vote up 5 votes
public Matrix getRotateMatrix(Bitmap bitmap, int rotation) {
    Matrix matrix = new Matrix();
    if (bitmap != null && rotation != 0) {
        int cx = bitmap.getWidth() / 2;
        int cy = bitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(rotation);
        matrix.postTranslate(bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    }
    return matrix;
}
 
Example 12
Project: GitHub   File: ScalingUtils.java   Source Code and License Vote up 5 votes
@Override
public void getTransformImpl(
    Matrix outTransform,
    Rect parentRect,
    int childWidth,
    int childHeight,
    float focusX,
    float focusY,
    float scaleX,
    float scaleY) {
  float dx = parentRect.left;
  float dy = parentRect.top;
  outTransform.setScale(scaleX, scaleY);
  outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
 
Example 13
Project: boohee_v5.6   File: AnimatorProxy.java   Source Code and License Vote up 5 votes
private void transformMatrix(Matrix m, View view) {
    float w = (float) view.getWidth();
    float h = (float) view.getHeight();
    boolean hasPivot = this.mHasPivot;
    float pX = hasPivot ? this.mPivotX : w / 2.0f;
    float pY = hasPivot ? this.mPivotY : h / 2.0f;
    float rX = this.mRotationX;
    float rY = this.mRotationY;
    float rZ = this.mRotationZ;
    if (!(rX == 0.0f && rY == 0.0f && rZ == 0.0f)) {
        Camera camera = this.mCamera;
        camera.save();
        camera.rotateX(rX);
        camera.rotateY(rY);
        camera.rotateZ(-rZ);
        camera.getMatrix(m);
        camera.restore();
        m.preTranslate(-pX, -pY);
        m.postTranslate(pX, pY);
    }
    float sX = this.mScaleX;
    float sY = this.mScaleY;
    if (!(sX == 1.0f && sY == 1.0f)) {
        m.postScale(sX, sY);
        m.postTranslate((-(pX / w)) * ((sX * w) - w), (-(pY / h)) * ((sY * h) - h));
    }
    m.postTranslate(this.mTranslationX, this.mTranslationY);
}
 
Example 14
Project: ImageEraser   File: MainActivity.java   Source Code and License Vote up 5 votes
public Bitmap resizeBitmapByCanvas() {
    float width;
    float heigth;
    float orginalWidth = (float) originalBitmap.getWidth();
    float orginalHeight = (float) originalBitmap.getHeight();
    if (orginalWidth > orginalHeight) {
        width = (float) imageViewWidth;
        heigth = (((float) imageViewWidth) * orginalHeight) / orginalWidth;
    } else {
        heigth = (float) imageViewHeight;
        width = (((float) imageViewHeight) * orginalWidth) / orginalHeight;
    }
    if (width > orginalWidth || heigth > orginalHeight) {
        return originalBitmap;
    }
    Bitmap background = Bitmap.createBitmap((int) width, (int) heigth, Config.ARGB_8888);
    Canvas canvas = new Canvas(background);
    float scale = width / orginalWidth;
    float yTranslation = (heigth - (orginalHeight * scale)) / 2.0f;
    Matrix transformation = new Matrix();
    transformation.postTranslate(0.0f, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(originalBitmap, transformation, paint);
    this.isImageResized = true;
    return background;
}
 
Example 15
Project: fast_face_android   File: OnGetImageListener.java   Source Code and License Vote up 5 votes
private void drawResizedBitmap(final Bitmap src, final Bitmap dst) {

        Display getOrient = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = Configuration.ORIENTATION_UNDEFINED;
        Point point = new Point();
        getOrient.getSize(point);
        int screen_width = point.x;
        int screen_height = point.y;
        Log.d(TAG, String.format("screen size (%d,%d)", screen_width, screen_height));
        if (screen_width < screen_height) {
            orientation = Configuration.ORIENTATION_PORTRAIT;
            mScreenRotation = -90;
        } else {
            orientation = Configuration.ORIENTATION_LANDSCAPE;
            mScreenRotation = 0;
        }

        Assert.assertEquals(dst.getWidth(), dst.getHeight());
        final float minDim = Math.min(src.getWidth(), src.getHeight());

        final Matrix matrix = new Matrix();

        // We only want the center square out of the original rectangle.
        final float translateX = -Math.max(0, (src.getWidth() - minDim) / 2);
        final float translateY = -Math.max(0, (src.getHeight() - minDim) / 2);
        matrix.preTranslate(translateX, translateY);

        final float scaleFactor = dst.getHeight() / minDim;
        matrix.postScale(scaleFactor, scaleFactor);

        // Rotate around the center if necessary.
        if (mScreenRotation != 0) {
            matrix.postTranslate(-dst.getWidth() / 2.0f, -dst.getHeight() / 2.0f);
            matrix.postRotate(mScreenRotation);
            matrix.postTranslate(dst.getWidth() / 2.0f, dst.getHeight() / 2.0f);
        }

        final Canvas canvas = new Canvas(dst);
        canvas.drawBitmap(src, matrix, null);
    }
 
Example 16
Project: Tensorflow_Andriod_With_Audio_Output   File: ClassifierActivity.java   Source Code and License Vote up 5 votes
private void renderDebug(final Canvas canvas) {
  if (!isDebug()) {
    return;
  }
  final Bitmap copy = cropCopyBitmap;
  if (copy != null) {
    final Matrix matrix = new Matrix();
    final float scaleFactor = 2;
    matrix.postScale(scaleFactor, scaleFactor);
    matrix.postTranslate(
        canvas.getWidth() - copy.getWidth() * scaleFactor,
        canvas.getHeight() - copy.getHeight() * scaleFactor);
    canvas.drawBitmap(copy, matrix, new Paint());

    final Vector<String> lines = new Vector<String>();
    if (classifier != null) {
      String statString = classifier.getStatString();
      String[] statLines = statString.split("\n");
      for (String line : statLines) {
        lines.add(line);
      }
    }

    lines.add("Frame: " + previewWidth + "x" + previewHeight);
    lines.add("Crop: " + copy.getWidth() + "x" + copy.getHeight());
    lines.add("View: " + canvas.getWidth() + "x" + canvas.getHeight());
    lines.add("Rotation: " + sensorOrientation);
    lines.add("Inference time: " + lastProcessingTimeMs + "ms");

    borderedText.drawLines(canvas, 10, canvas.getHeight() - 10, lines);
  }
}
 
Example 17
Project: android-Ultra-Pull-To-Refresh   File: StoreHouseHeader.java   Source Code and License Vote up 4 votes
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float progress = mProgress;
    int c1 = canvas.save();
    int len = mItemList.size();

    for (int i = 0; i < len; i++) {

        canvas.save();
        StoreHouseBarItem storeHouseBarItem = mItemList.get(i);
        float offsetX = mOffsetX + storeHouseBarItem.midPoint.x;
        float offsetY = mOffsetY + storeHouseBarItem.midPoint.y;

        if (mIsInLoading) {
            storeHouseBarItem.getTransformation(getDrawingTime(), mTransformation);
            canvas.translate(offsetX, offsetY);
        } else {

            if (progress == 0) {
                storeHouseBarItem.resetPosition(mHorizontalRandomness);
                continue;
            }

            float startPadding = (1 - mInternalAnimationFactor) * i / len;
            float endPadding = 1 - mInternalAnimationFactor - startPadding;

            // done
            if (progress == 1 || progress >= 1 - endPadding) {
                canvas.translate(offsetX, offsetY);
                storeHouseBarItem.setAlpha(mBarDarkAlpha);
            } else {
                float realProgress;
                if (progress <= startPadding) {
                    realProgress = 0;
                } else {
                    realProgress = Math.min(1, (progress - startPadding) / mInternalAnimationFactor);
                }
                offsetX += storeHouseBarItem.translationX * (1 - realProgress);
                offsetY += -mDropHeight * (1 - realProgress);
                Matrix matrix = new Matrix();
                matrix.postRotate(360 * realProgress);
                matrix.postScale(realProgress, realProgress);
                matrix.postTranslate(offsetX, offsetY);
                storeHouseBarItem.setAlpha(mBarDarkAlpha * realProgress);
                canvas.concat(matrix);
            }
        }
        storeHouseBarItem.draw(canvas);
        canvas.restore();
    }
    if (mIsInLoading) {
        invalidate();
    }
    canvas.restoreToCount(c1);
}
 
Example 18
Project: TensorflowAndroidDemo   File: ImageUtils.java   Source Code and License Vote up 4 votes
/**
 * Returns a transformation matrix from one reference frame into another.
 * Handles cropping (if maintaining aspect ratio is desired) and rotation.
 *
 * @param srcWidth Width of source frame.
 * @param srcHeight Height of source frame.
 * @param dstWidth Width of destination frame.
 * @param dstHeight Height of destination frame.
 * @param applyRotation Amount of rotation to apply from one frame to another.
 *  Must be a multiple of 90.
 * @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
 * cropping the image if necessary.
 * @return The transformation fulfilling the desired requirements.
 */
public static Matrix getTransformationMatrix(
    final int srcWidth,
    final int srcHeight,
    final int dstWidth,
    final int dstHeight,
    final int applyRotation,
    final boolean maintainAspectRatio) {
  final Matrix matrix = new Matrix();

  if (applyRotation != 0) {
    // Translate so center of image is at origin.
    matrix.postTranslate(-srcWidth / 2.0f, -srcHeight / 2.0f);

    // Rotate around origin.
    matrix.postRotate(applyRotation);
  }

  // Account for the already applied rotation, if any, and then determine how
  // much scaling is needed for each axis.
  final boolean transpose = (Math.abs(applyRotation) + 90) % 180 == 0;

  final int inWidth = transpose ? srcHeight : srcWidth;
  final int inHeight = transpose ? srcWidth : srcHeight;

  // Apply scaling if necessary.
  if (inWidth != dstWidth || inHeight != dstHeight) {
    final float scaleFactorX = dstWidth / (float) inWidth;
    final float scaleFactorY = dstHeight / (float) inHeight;

    if (maintainAspectRatio) {
      // Scale by minimum factor so that dst is filled completely while
      // maintaining the aspect ratio. Some image may fall off the edge.
      final float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
      matrix.postScale(scaleFactor, scaleFactor);
    } else {
      // Scale exactly to fill dst from src.
      matrix.postScale(scaleFactorX, scaleFactorY);
    }
  }

  if (applyRotation != 0) {
    // Translate back from origin centered reference to destination frame.
    matrix.postTranslate(dstWidth / 2.0f, dstHeight / 2.0f);
  }

  return matrix;
}
 
Example 19
Project: decoy   File: BaseZoomableImageView.java   Source Code and License Vote up 3 votes
/**
 *  Setup the base matrix so that the image is centered and scaled properly.
 * 根据Bitmap和Rect,设置初始的显示矩阵Matrix
 * @author Linleja
 * @date 2014-4-29
 * @param bitmap
 * @param matrix
 * @param selection
 */
private void setBaseMatrix(Bitmap bitmap, Matrix matrix,Rect selection) {
	if(selection == null){
		return ;
	}

	float viewWidth = selection.right - selection.left;
	float viewHeight = selection.bottom - selection.top;

	matrix.reset();



	float widthRatio = viewWidth / (float)bitmap.getWidth();
	float heighRatio = viewHeight / (float)bitmap.getHeight();
	float scale = 1.0f;
	if (widthRatio > heighRatio) {
		scale = widthRatio;


	} else {
		scale = heighRatio;


	}

	matrix.setScale(scale, scale);
	matrix.postTranslate(
			((getWidth()  - (float)bitmap.getWidth()  * scale))/2F, 
			((getHeight() - (float)bitmap.getHeight() * scale))/2F);
}
 
Example 20
Project: VectorMaster   File: VectorMasterView.java   Source Code and License Vote up 3 votes
void buildScaleMatrix() {

        scaleMatrix = new Matrix();

        scaleMatrix.postTranslate(width / 2 - vectorModel.getViewportWidth() / 2, height / 2 - vectorModel.getViewportHeight() / 2);

        float widthRatio = width / vectorModel.getViewportWidth();
        float heightRatio = height / vectorModel.getViewportHeight();
        float ratio = Math.min(widthRatio, heightRatio);

        scaleRatio = ratio;

        scaleMatrix.postScale(ratio, ratio, width / 2, height / 2);
    }