Code example for Matrix

Methods: equals, mapRect, postConcat, set

0
                matrix = null;
            } 
         
            // don't invalidate unless we're actually changing our matrix 
            if (matrix == null && !super.getImageMatrix().isIdentity() ||
                    matrix != null && !super.getImageMatrix().equals(matrix)) {
//ALPS00126955 BEGIN 
                //when we invalidate,call super.setImageMatrix(),not set super's matrix ourselves. 
                //super.getImageMatrix().set(matrix); 
                setImageMatrix(matrix);
//ALPS00126955 END 
            } 
        } 
    } 
//Gipi Add - E 
 
    @Override 
    protected void onLayout(boolean changed, int left, int top,
                            int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mThisWidth = right - left;
        mThisHeight = bottom - top;
        Runnable r = mOnLayoutRunnable;
        if (r != null) {
            mOnLayoutRunnable = null;
            r.run();
        } 
        if (mBitmapDisplayed.getBitmap() != null) {
            getProperBaseMatrix(mBitmapDisplayed, mBaseMatrix);
            //setImageMatrix(getImageViewMatrix()); 
            setImageMatrix(getImageViewMatrix() , false); 
        } 
		if(changed) {
			centerInZoom(); 
		} 
    } 
 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            event.startTracking();
            return true; 
        } 
        return super.onKeyDown(keyCode, event);
    } 
 
    @Override 
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
                && !event.isCanceled()) {
            if (getScale() > 1.0f) { 
                // If we're zoomed in, pressing Back jumps out to show the 
                // entire image, otherwise Back returns the user to the gallery. 
                zoomTo(1.0f); 
                return true; 
            } 
        } 
        return super.onKeyUp(keyCode, event);
    } 
 
    protected Handler mHandler = new Handler();
 
    @Override 
    public void setImageBitmap(Bitmap bitmap) {
        setImageBitmap(bitmap, 0);
    } 
 
    private void setImageBitmap(Bitmap bitmap, int rotation) {
        super.setImageBitmap(bitmap);
        Drawable d = getDrawable();
        if (d != null) {
            d.setDither(true);
        } 
 
        Bitmap old = mBitmapDisplayed.getBitmap();
        mBitmapDisplayed.setBitmap(bitmap);
        mBitmapDisplayed.setRotation(rotation);
 
        if (old != null && old != bitmap && mRecycler != null) {
            mRecycler.recycle(old);
        } 
    } 
 
    public void clear() { 
        setImageBitmapResetBase(null, true); 
    } 
 
    private Runnable mOnLayoutRunnable = null;
 
    // This function changes bitmap, reset base matrix according to the size 
    // of the bitmap, and optionally reset the supplementary matrix. 
    public void setImageBitmapResetBase(final Bitmap bitmap,
            final boolean resetSupp) {
        setImageRotateBitmapResetBase(new RotateBitmap(bitmap), resetSupp);
    } 
 
    public void setImageRotateBitmapResetBase(final RotateBitmap bitmap,
            final boolean resetSupp) {
        final int viewWidth = getWidth();
 
        if (viewWidth <= 0)  {
            mOnLayoutRunnable = new Runnable() {
                public void run() { 
                    setImageRotateBitmapResetBase(bitmap, resetSupp, true); 
                } 
            }; 
            return; 
        } 
 
        if (bitmap.getBitmap() != null) {
            getProperBaseMatrix(bitmap, mBaseMatrix);
            setImageBitmap(bitmap.getBitmap(), bitmap.getRotation());
        } else { 
            mBaseMatrix.reset();
            setImageBitmap(null); 
        } 
 
        if (resetSupp) {
            mSuppMatrix.reset();
        } 
 
//Gipi Add - S 
        setImageMatrix(getImageViewMatrix() , false); 
//Gipi Add - E 
 
        mMaxZoom = maxZoom();
    } 
 
    public void setImageRotateBitmapResetBase(final RotateBitmap bitmap,
            final boolean resetSupp, final boolean invalidate) {
        final int viewWidth = getWidth();
 
        if (viewWidth <= 0)  {
            mOnLayoutRunnable = new Runnable() {
                public void run() { 
                    setImageRotateBitmapResetBase(bitmap, resetSupp);
                } 
            }; 
            return; 
        } 
 
        if (bitmap.getBitmap() != null) {
            getProperBaseMatrix(bitmap, mBaseMatrix);
            setImageBitmap(bitmap.getBitmap(), bitmap.getRotation());
        } else { 
            mBaseMatrix.reset();
            setImageBitmap(null); 
        } 
 
        if (resetSupp) {
            mSuppMatrix.reset();
        } 
 
//Gipi Add - S 
        //setImageMatrix(getImageViewMatrix()); 
        setImageMatrix(getImageViewMatrix() , invalidate);
//Gipi Add - E 
 
        mMaxZoom = maxZoom();
    } 
 
 
//Gipi add - S 
    private void centerInZoom() { 
        if (mBitmapDisplayed.getBitmap() == null) {
            return; 
        } 
 
        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;
 
            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;
            } 
 
            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() , false); 
    } 
//Gipi add - E 
 
    // Center as much as possible in one or both axis.  Centering is 
    // defined as follows:  if the image is scaled down below the 
    // view's dimensions then center it (literally).  If the image 
    // is scaled larger than the view and is translated out of view 
    // then translate it back into view (i.e. eliminate black bars). 
    protected void center(boolean horizontal, boolean vertical) {
        if (mBitmapDisplayed.getBitmap() == null) {
            return; 
        } 
 
        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 3x otherwise the result may look bad if it's 
        // a small icon. 
        float widthScale = Math.min(viewWidth / w, 3.0f);
        float heightScale = Math.min(viewHeight / h, 3.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);
    } 
 
//Gipi add - S 
    public int getProperWindowSize(boolean isInit)
    { 
        int viewHeight = getHeight();
        int viewWidth = getWidth();
		WindowManager wm = (WindowManager)(getContext().getSystemService(Context.WINDOW_SERVICE));
        if ( viewWidth == 0  ){
            viewWidth = wm.getDefaultDisplay().getWidth();
        } 
        if ( viewHeight == 0 ){
            viewHeight = wm.getDefaultDisplay().getHeight();
        } 
 
        viewWidth = (viewWidth > viewHeight) ? viewHeight : viewWidth;
//android.util.Log.d("Scale","Ratio:"+getScale()); 
        if(isInit)
        { 
            return viewWidth;
        }else 
        { 
            return viewWidth*Math.round(getScale());
        } 
    } 
//Gipi add - E 
 
    // 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;