android imageview 获取bitmap缩放大小
时间: 2023-04-09 08:04:25 浏览: 167
可以使用以下代码获取 ImageView 中的 Bitmap 缩放大小:
```
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = imageView.getWidth();
int height = imageView.getHeight();
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
float scaleX = (float) width / bitmapWidth;
float scaleY = (float) height / bitmapHeight;
float scale = Math.min(scaleX, scaleY);
int scaledWidth = (int) (bitmapWidth * scale);
int scaledHeight = (int) (bitmapHeight * scale);
// scaledWidth 和 scaledHeight 就是缩放后的大小
}
});
```
阅读全文