Code example for ImageView

Methods: getDrawablesetImageMatrix, getHeight, getScaleType, getWidth

0
		final int viewWidth = imageView.getWidth();
		if (width <= viewWidth) {
			switch (mScaleType) {
				case FIT_START:
					deltaX = -rect.left;
					break; 
				case FIT_END:
					deltaX = viewWidth - width - rect.left;
					break; 
				default: 
					deltaX = (viewWidth - width) / 2 - rect.left;
					break; 
			} 
			mScrollEdge = EDGE_BOTH;
		} else if (rect.left > 0) {
			mScrollEdge = EDGE_LEFT;
			deltaX = -rect.left;
		} else if (rect.right < viewWidth) {
			deltaX = viewWidth - rect.right;
			mScrollEdge = EDGE_RIGHT;
		} else { 
			mScrollEdge = EDGE_NONE;
		} 
 
		// Finally actually translate the matrix 
		mSuppMatrix.postTranslate(deltaX, deltaY);
	} 
 
	/** 
	 * Helper method that maps the supplied Matrix to the current Drawable 
	 *  
	 * @param matrix - Matrix to map Drawable against 
	 * @return RectF - Displayed Rectangle 
	 */ 
	private RectF getDisplayRect(Matrix matrix) {
		ImageView imageView = getImageView();
 
		if (null != imageView) {
			Drawable d = imageView.getDrawable();
			if (null != d) {
				mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
				matrix.mapRect(mDisplayRect);
				return mDisplayRect;
			} 
		} 
		return null; 
	} 
 
	/** 
	 * Helper method that 'unpacks' a Matrix and returns the required value 
	 *  
	 * @param matrix - Matrix to unpack 
	 * @param whichValue - Which value from Matrix.M* to return 
	 * @return float - returned value 
	 */ 
	private float getValue(Matrix matrix, int whichValue) {
		matrix.getValues(mMatrixValues);
		return mMatrixValues[whichValue];
	} 
 
	/** 
	 * Resets the Matrix back to FIT_CENTER, and then displays it.s 
	 */ 
	private void resetMatrix() { 
		mSuppMatrix.reset();
		setImageViewMatrix(getDisplayMatrix()); 
		checkMatrixBounds(); 
	} 
 
	private void setImageViewMatrix(Matrix matrix) {
		ImageView imageView = getImageView();
		if (null != imageView) {
 
			checkImageViewScaleType(); 
			imageView.setImageMatrix(matrix);
 
			// Call MatrixChangedListener if needed 
			if (null != mMatrixChangeListener) {
				RectF displayRect = getDisplayRect(matrix);
				if (null != displayRect) {
					mMatrixChangeListener.onMatrixChanged(displayRect);
				} 
			} 
		} 
	} 
 
	/** 
	 * Calculate Matrix for FIT_CENTER 
	 *  
	 * @param d - Drawable being displayed 
	 */ 
	private void updateBaseMatrix(Drawable d) {
		ImageView imageView = getImageView();
		if (null == imageView || null == d) {
			return; 
		} 
 
		final float viewWidth = imageView.getWidth();
		final float viewHeight = imageView.getHeight();
		final int drawableWidth = d.getIntrinsicWidth();
		final int drawableHeight = d.getIntrinsicHeight();
 
		mBaseMatrix.reset();
 
		final float widthScale = viewWidth / drawableWidth;
		final float heightScale = viewHeight / drawableHeight;
 
		if (mScaleType == ScaleType.CENTER) {
			mBaseMatrix.postTranslate((viewWidth - drawableWidth) / 2F, (viewHeight - drawableHeight) / 2F);
 
		} else if (mScaleType == ScaleType.CENTER_CROP) {
			float scale = Math.max(widthScale, heightScale);
			mBaseMatrix.postScale(scale, scale);
			mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F,
					(viewHeight - drawableHeight * scale) / 2F);
 
		} else if (mScaleType == ScaleType.CENTER_INSIDE) {
			float scale = Math.min(1.0f, Math.min(widthScale, heightScale));
			mBaseMatrix.postScale(scale, scale);
			mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F,
					(viewHeight - drawableHeight * scale) / 2F);
 
		} else { 
			RectF mTempSrc = new RectF(0, 0, drawableWidth, drawableHeight);
			RectF mTempDst = new RectF(0, 0, viewWidth, viewHeight);
 
			switch (mScaleType) {
				case FIT_CENTER:
					mBaseMatrix.setRectToRect(mTempSrc, mTempDst, ScaleToFit.CENTER);