Code example for Matrix

Methods: mapRect, postConcat, set

0
 
        RectF rect = new RectF(0, 0,
                mBitmapDisplayed.getWidth(),
                mBitmapDisplayed.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);
    } 
 
    public float getScale() { 
        return getScale(mSuppMatrix);
    } 
 
    // Setup the base matrix so that the image is centered and scaled properly. 
    // 现修改为缩放以宽缩放比例为基准 
    private void getProperBaseMatrix(Matrix matrix) {
        float viewWidth = getWidth();
        float viewHeight = getHeight();
 
        float w = getBitmapDisplayedWidth();
        float h = getBitmapDisplayedHeight();
        matrix.reset();
 
        float widthScale = Math.min(viewWidth / w, 1.0f);
        // float heightScale = Math.min(viewHeight / h, 1.0f); 
        // float scale = Math.min(widthScale, heightScale); 
        float scale = widthScale;
 
        matrix.postConcat(getRotateMatrix());
        matrix.postScale(scale, scale);
 
        matrix.postTranslate(
                (viewWidth  - w * scale) / 2F,
                // (viewHeight - h * scale) / 2F); 
                Math.max((viewHeight - h * scale), 0) / 2F);
    } 
 
    private Matrix getRotateMatrix() {
        // By default this is an identity matrix. 
        Matrix matrix = new Matrix();
        if (mRotation != 0 && mBitmapDisplayed != null) {
            // We want to do the rotation at origin, but since the bounding 
            // rectangle will be changed after rotation, so the delta values 
            // are based on old & new width/height respectively. 
            int cx = mBitmapDisplayed.getWidth() / 2;
            int cy = mBitmapDisplayed.getHeight() / 2;
            matrix.preTranslate(-cx, -cy);
            matrix.postRotate(mRotation);
            matrix.postTranslate(getBitmapDisplayedWidth() / 2, getBitmapDisplayedHeight() / 2);
        } 
        return matrix;
    } 
 
    // Combine the base matrix and the supp matrix to make the final matrix. 
    // 基本矩阵和增补矩阵连接运算得到最终矩阵 
    protected Matrix getImageViewMatrix() {
        // The final matrix is computed as the concatenation of the base matrix 
        // and the supplementary matrix. 
    	// 最终矩阵由基矩阵和增补矩阵连接运算而成 
        mDisplayMatrix.set(mBaseMatrix);
        mDisplayMatrix.postConcat(mSuppMatrix);
        return mDisplayMatrix;
    } 
 
    static final float SCALE_RATE = 1.25F;