ImageView imageView = (ImageView) findViewById(R.id.imageView);
int imageViewWidth = imageView.getWidth();
int imageViewHeight = imageView.getHeight();
int targetWidth = 500;
int targetHeight = 500;
int anchorX = (int) (imageViewWidth * 0.5);
int anchorY = (int) (imageViewHeight * 0.5);
Matrix matrix = new Matrix();
matrix.postScale((float) targetWidth / imageViewWidth, (float) targetHeight / imageViewHeight, anchorX, anchorY);
matrix.postTranslate((imageViewWidth - targetWidth) / 2, (imageViewHeight - targetHeight) / 2);
imageView.setImageMatrix(matrix);
完整的代码示例:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
int imageViewWidth = imageView.getWidth();
int imageViewHeight = imageView.getHeight();
int targetWidth = 500;
int targetHeight = 500;
int anchorX = (int) (imageViewWidth * 0.5);
int anchorY = (int) (imageViewHeight * 0.5);
Matrix matrix = new Matrix();
matrix.postScale((float) targetWidth / imageViewWidth, (float) targetHeight / imageViewHeight, anchorX, anchorY);
matrix.postTranslate((imageViewWidth - targetWidth) / 2, (imageViewHeight - targetHeight) / 2);
imageView.setImageMatrix(matrix);