如何放大/缩小图像并始终填充视口?

时间:2018-08-22 12:00:27

标签: android android-imageview

我有一个ImageView,它的宽度和高度都固定。我将图像加载到其中。现在,我要放大和缩小图像。但是图像应始终填充视口。我尝试了以下在CustomImageView类中编写的代码。

Matrix m = getImageMatrix();
Drawable d = getDrawable();
//Get the image's rect
RectF drawableRect = new RectF(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//Get the image view's rect
RectF viewRect = new RectF(0, 0, getWidth(), getHeight());
//draw the image in the view
m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER);
float scaleWidth = (float) getWidth() / d.getIntrinsicWidth() + 0.1f;
float scaleHeight = (float) getHeight() / d.getIntrinsicHeight(); + 0.1f
m.postScale(scaleWidth, scaleHeight, getWidth() / 2, getHeight() / 2);
setImageMatrix(m);
invalidate();

我将在循环中运行此代码,这会产生放大和缩小的错觉。但是图像没有填满视口。我应该写些什么,以便每次执行此操作时,图像都应填满视口?

0 个答案:

没有答案