Code example for Matrix

Methods: mapRect, postRotate, postScale, postTranslate, reset

0
     * @see android.widget.ImageView#setImageBitmap(android.graphics.Bitmap) 
     */ 
    @Override 
    public void setImageBitmap(Bitmap bitmap)
    { 
        Matrix matrix = new Matrix();
        matrix.reset();
        // apply the camera display characteristics. First mirror the image... 
        if (mbMirror)
        { 
            matrix.postScale(-1f, 1f);
        } 
        // then rotate it... 
        matrix.postRotate(mnDisplayOrientation);
        /** 
         * Math is hard!  
         *         -- Barbie 
         *  
         * We could calculate the matrix that will map the rotated and possibly 
         * mirrored image to the display, but it is much easier to compose a 
         * matrix that gets the width and height right, and then translate it so 
         * the top left corner maps to (0,0). This will correctly fit the image 
         * to the display. 
         *  
         * Rotated image width and height must match display width and height 
         * see where the image rectangle maps now. 
         */ 
        RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        matrix.mapRect(rect);
        switch (mScaleType)
        { 
            case FIT_XY:
                // get the width and height of the image right 
                matrix.postScale(this.getWidth() / rect.width(),
                        this.getHeight() / rect.height());
                break; 
            case CENTER_CROP:
            /* 
             * "Scale the image uniformly (maintain the image's aspect ratio) so 
             * that both dimensions (width and height) of the image will be 
             * equal to or LARGER than the corresponding dimension of the view 
             * (minus padding)." (Android documentation) 
             */ 
            { 
                float scale = Math.max(this.getWidth() / rect.width(),
                        this.getHeight() / rect.height());
                matrix.postScale(scale, scale);
                break; 
            } 
            case CENTER:
                // everything is done below in if statement 
                // translate center of image to center of display 
                break; 
            case CENTER_INSIDE:
            case FIT_START:
            case FIT_END:
            case FIT_CENTER:
            { 
                /** 
                 * "Scale the image uniformly (maintain the image's aspect 
                 * ratio) so that both dimensions (width and height) of the 
                 * image will be equal to or LESS than the corresponding 
                 * dimension of the view (minus padding)." (Android documentation) 
                 */ 
                float scale = Math.min(this.getWidth() / rect.width(),
                        this.getHeight() / rect.height());
                matrix.postScale(scale, scale);
                break; 
            } 
        } 
        // we translate the image to obtain the other part of the postcondition. 
        // Get the mapping of the image rectangle before translation. 
        rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
        matrix.mapRect(rect);
        if (mScaleType == ScaleType.CENTER
                || mScaleType == ScaleType.FIT_CENTER)
        { 
            // translate center of image to center of display 
            matrix.postTranslate(this.getWidth() / 2 - rect.centerX(),
                    this.getHeight() / 2 - rect.centerY());
 
        } else if (mScaleType == ScaleType.FIT_START
                || mScaleType == ScaleType.FIT_XY)
        { 
            // now translate the image so the top left corner maps to (0,0) 
            matrix.postTranslate(-rect.left, -rect.top);
        } else if (mScaleType == ScaleType.FIT_END)
        { 
            // translate the image so the bottom right corner maps to (width(), 
            // height()) 
            matrix.postTranslate(getWidth() - rect.width(),
                    getHeight() - rect.height());
        } 
        super.setScaleType(ScaleType.MATRIX);
        super.setImageMatrix(matrix);
        super.setImageBitmap(bitmap);