small medium large xlarge

24 Mar 2012, 14:26
Neeraj Narang (1 post)

I tried implementing the code for zoom image using a matrix in android. But am getting the below issue. Could someone please help me out with this?

public class MyActivity extends Activity{ Matrix matrix = new Matrix(); Matrix savedMatrix = new Matrix(); float scaleWidth, scaleHeight,screenWidth,screenHeight; ImageView imageView;

public void onCreate(Bundle savedInstanceState) {
    Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    imageView = (ImageView) findViewById(;
    Bitmap cur_bm = BitmapFactory.decodeResource(getResources(),
} }

If I comment the line imageView.setScaleType(ScaleType.MATRIX); then the image is skewed. It doesn’t fill the screen.

But if I uncomment the line imageView.setScaleType(ScaleType.MATRIX); then the image fills the screen.

Just setting the scaleType to MATRIX causes the image to scale down. Could someone please tell why this happens and how to overcome this issue?

12 Apr 2012, 17:56
Ed Burnette (1341 posts)

Neeraj and I exchanged some email and he says this code does what he wants now:

Matrix m = imageView.getImageMatrix();

//Get the image’s rect RectF drawableRect = new RectF(0, 0, DrawView.imageWidth.floatValue(), DrawView.imageHeight.floatValue());

//Get the image view’s rect RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight()); m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);

//set scale m.postScale(scaleWidth.floatValue(), scaleHeight.floatValue(), x, y); imageView.setImageMatrix(m);