I’ve the following code for implementing zoom image in Android:
public class MyActivity extends Activity{
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
float scaleWidth, scaleHeight,screenWidth,screenHeight;
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display display = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
screenWidth=display.getWidth();
screenHeight=display.getHeight();
imageView = (ImageView) findViewById(R.id.imageview);
imageView.setScaleType(ScaleType.MATRIX);
Bitmap cur_bm = BitmapFactory.decodeResource(getResources(),
R.drawable.myimage);
float imageWidth = (float) cur_bm.getWidth();
float imageHeight = (float) cur_bm.getHeight();
float newHeight = screenHeight;
float newWidth = screenWidth;
scaleWidth = screenWidth / imageWidth;
scaleHeight = newHeight / imageHeight;
imageView.setImageBitmap(cur_bm);
SetImageMatrix();
}
void SetImageMatrix() {
Matrix mtrx = new Matrix();
scaleWidth+=0.6;
scaleHeight+=0.6;
mtrx.postScale(scaleWidth, scaleHeight,screenWidth/2, screenHeight/2);
imageView.setImageMatrix(mtrx);
imageView.invalidate();
}
}
If I comment the line imageView.setScaleType(ScaleType.MATRIX); and comment out the function SetImageMatrix(), then the image appears as follows in the emulator:

But if I uncomment the line imageView.setScaleType(ScaleType.MATRIX); and keep the function commented, then the image appears as follows in the emulator:

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?
Finally, I found the answer to this on one of the posts’…