Code example for Matrix

Methods: isIdentity, mapPoints, postConcat, set

0
		} 
	} 
 
	// Translate a given point through a given matrix. 
	static private void translatePoint(Matrix matrix, float [] xy) {
		matrix.mapPoints(xy);
	} 
 
	// Identical to the setImageMatrix method in ImageView 
	public void setImageMatrix(Matrix m){
		if (m != null && m.isIdentity()) {
			m = null;
		} 
 
		// don't invalidate unless we're actually changing our matrix 
		if (m == null && !this.mMatrix.isIdentity() || m != null && !this.mMatrix.equals(m)) {
			this.mMatrix.set(m);
			invalidate();
		} 
	} 
 
	// Sets the bitmap for the image and resets the base 
	public void setImageBitmap(final Bitmap bitmap) {
		final int viewWidth = getWidth();
		 
		if( Build.VERSION.SDK_INT >=  Build.VERSION_CODES.HONEYCOMB && bitmap!=null && bitmap.getHeight()>1800 )
			setLayerType(View.LAYER_TYPE_SOFTWARE, null);
 
		if (viewWidth <= 0)  {
			mOnLayoutRunnable = new Runnable() {
				public void run() { 
					setImageBitmap(bitmap);
				} 
			}; 
			return; 
		} 
 
		if (bitmap != null) {
			setBaseMatrix(bitmap, mBaseMatrix);
			this.mBitmap = bitmap;
		} else { 
			mBaseMatrix.reset();
			this.mBitmap = bitmap;
		} 
 
		mSuppMatrix.reset();
		setImageMatrix(getImageViewMatrix()); 
		mMaxZoom = maxZoom();
		 
		// Set the image to fit the screen 
		zoomTo(zoomDefault()); 
	} 
 
	// Unchanged from ImageViewTouchBase 
	// 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 vertical, boolean horizontal, boolean animate) {
		if (mBitmap == null)
			return; 
 
		Matrix m = getImageViewMatrix();
 
		float [] topLeft  = new float[] { 0, 0 };
		float [] botRight = new float[] { mBitmap.getWidth(), mBitmap.getHeight() };
 
		translatePoint(m, topLeft);
		translatePoint(m, botRight);
 
		float height = botRight[1] - topLeft[1];
		float width  = botRight[0] - topLeft[0];
 
		float deltaX = 0, deltaY = 0;
 
		if (vertical) {
			int viewHeight = getHeight();
			if (height < viewHeight) {
				deltaY = (viewHeight - height)/2 - topLeft[1];
			} else if (topLeft[1] > 0) {
				deltaY = -topLeft[1];
			} else if (botRight[1] < viewHeight) {
				deltaY = getHeight() - botRight[1];
			} 
		} 
 
		if (horizontal) {
			int viewWidth = getWidth();
			if (width < viewWidth) {
				deltaX = (viewWidth - width)/2 - topLeft[0];
			} else if (topLeft[0] > 0) {
				deltaX = -topLeft[0];
			} else if (botRight[0] < viewWidth) {
				deltaX = viewWidth - botRight[0];
			} 
		} 
 
		postTranslate(deltaX, deltaY);
		if (animate) {
			Animation a = new TranslateAnimation(-deltaX, 0, -deltaY, 0);
			a.setStartTime(SystemClock.elapsedRealtime());
			a.setDuration(250);
			setAnimation(a);
		} 
		setImageMatrix(getImageViewMatrix()); 
	} 
 
	// Unchanged from ImageViewTouchBase 
	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) {
 
		// If the bitmap is set return the scale 
		if(mBitmap!=null)
			return getValue(matrix, Matrix.MSCALE_X);
		// Otherwise return the default value of 1 
		else 
			return 1f; 
	} 
 
	// Returns the current scale of the view  
	public float getScale() { 
		return getScale(mSuppMatrix);
	} 
 
	// Setup the base matrix so that the image is centered and scaled properly. 
	private void setBaseMatrix(Bitmap bitmap, Matrix matrix) {
		float viewWidth = getWidth();
		float viewHeight = getHeight();
 
		matrix.reset();
		float widthScale = Math.min(viewWidth / (float)bitmap.getWidth(), 1.0f);
		float heightScale = Math.min(viewHeight / (float)bitmap.getHeight(), 1.0f);
		float scale;
		if (widthScale > heightScale) {
			scale = heightScale;
		} else { 
			scale = widthScale;
		} 
		matrix.setScale(scale, scale);
		matrix.postTranslate(
				(viewWidth  - ((float)bitmap.getWidth()  * scale))/2F, 
				(viewHeight - ((float)bitmap.getHeight() * scale))/2F);
	} 
 
 
	// Combine the base matrix and the supp matrix to make the final matrix. 
	protected Matrix getImageViewMatrix() {
		mDisplayMatrix.set(mBaseMatrix);
		mDisplayMatrix.postConcat(mSuppMatrix);
		return mDisplayMatrix;
	} 
 
	// Sets the maximum zoom, which is a scale relative to the base matrix. It is calculated to show 
	// the image at 400% zoom regardless of screen or image orientation. If in the future we decode