Code example for Matrix

Methods: mapRect, postConcat, set

0
 
        Matrix m = getImageViewMatrix();
 
        RectF rect = new RectF(0, 0, mBitmapDisplayed.getBitmap().getWidth(), mBitmapDisplayed.getBitmap().getHeight());
 
        m.mapRect(rect);
 
        float height = rect.height();
        float width = rect.width();
 
        float deltaX = 0, deltaY = 0;
 
        if (vertical) {
            int viewHeight = getHeight();
            if (height < viewHeight) {
                deltaY = (viewHeight - height) / 2 - rect.top;
            } else if (rect.top > 0) {
                deltaY = -rect.top;
            } else if (rect.bottom < viewHeight) {
                deltaY = getHeight() - rect.bottom;
            } 
        } 
 
        if (horizontal) {
            int viewWidth = getWidth();
            if (width < viewWidth) {
                deltaX = (viewWidth - width) / 2 - rect.left;
            } else if (rect.left > 0) {
                deltaX = -rect.left;
            } else if (rect.right < viewWidth) {
                deltaX = viewWidth - rect.right;
            } 
        } 
 
        postTranslate(deltaX, deltaY);
        setImageMatrix(getImageViewMatrix());
    } 
 
    public ImageViewTouchBase(Context context) {
        super(context);
        init(); 
    } 
 
    public ImageViewTouchBase(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(); 
    } 
 
    private void init() { 
        setScaleType(ImageView.ScaleType.MATRIX);
    } 
 
    protected float getValue(Matrix matrix, int whichValue) {
        matrix.getValues(mMatrixValues);
        return mMatrixValues[whichValue];
    } 
 
    // Get the scale factor out of the matrix. 
    protected float getScale(Matrix matrix) {
        return getValue(matrix, Matrix.MSCALE_X);
    } 
 
    protected float getScale() { 
        return getScale(mSuppMatrix);
    } 
 
    // Setup the base matrix so that the image is centered and scaled properly. 
    private void getProperBaseMatrix(RotateBitmap bitmap, Matrix matrix) {
        float viewWidth = getWidth();
        float viewHeight = getHeight();
 
        float w = bitmap.getWidth();
        float h = bitmap.getHeight();
        matrix.reset();
 
        // We limit up-scaling to 2x otherwise the result may look bad if it's 
        // a small icon. 
        float widthScale = Math.min(viewWidth / w, 2.0f);
        float heightScale = Math.min(viewHeight / h, 2.0f);
        float scale = Math.min(widthScale, heightScale);
 
        matrix.postConcat(bitmap.getRotateMatrix());
        matrix.postScale(scale, scale);
 
        matrix.postTranslate((viewWidth - w * scale) / 2F, (viewHeight - h * scale) / 2F);
    } 
 
    // Combine the base matrix and the supp matrix to make the final matrix. 
    protected Matrix getImageViewMatrix() {
        // The final matrix is computed as the concatentation of the base matrix 
        // and the supplementary matrix. 
        mDisplayMatrix.set(mBaseMatrix);
        mDisplayMatrix.postConcat(mSuppMatrix);
        return mDisplayMatrix;
    } 
 
    static final float SCALE_RATE = 1.25F;