Android中心缩放代码
时间: 2023-11-03 12:22:46 浏览: 42
Android中心缩放代码可以通过Matrix类来实现。具体实现步骤如下:
1. 获取要缩放的View对象,例如ImageView。
2. 创建Matrix对象。
3. 计算缩放比例。
```
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
```
4. 设置Matrix对象的缩放比例。
```
matrix.postScale(scaleWidth, scaleHeight, viewWidth / 2f, viewHeight / 2f);
```
5. 将Matrix对象设置给View对象。
```
view.setImageMatrix(matrix);
```
完整代码如下:
```
ImageView view = findViewById(R.id.imageView);
Matrix matrix = new Matrix();
int width = view.getDrawable().getIntrinsicWidth();
int height = view.getDrawable().getIntrinsicHeight();
int viewWidth = view.getWidth();
int viewHeight = view.getHeight();
float scaleWidth = ((float) viewWidth) / width;
float scaleHeight = ((float) viewHeight) / height;
matrix.postScale(scaleWidth, scaleHeight, viewWidth / 2f, viewHeight / 2f);
view.setImageMatrix(matrix);
```
以上代码可以将ImageView对象按照中心缩放到View的大小。