0

我需要为以下结果制定正确的缩放方法:

ImageView 应缩放到:

  • 填充 X 轴
  • 保持纵横比
  • 将 ImageView 定位在 0,0
  • 在底部裁剪剩余部分。

我尝试了多种Matrix类型,但迄今为止未能达到结果。

4

1 回答 1

0

我也有同样的问题,我的解决方法是:

    ImageView imageView = (ImageView) findViewById(R.id.imageView);
    Matrix matrix = new Matrix();

    float imageWidth = image_original_width * display.density, imageHeight = image_original_height * display.density;
    float placeWidth = imageView.getWidth(), placeHeight = imageView.getHeight();
    float scaleToWidth = placeWidth / imageWidth;
    float scaledImageHeight = imageHeight * scaleToWidth;

    matrix.setScale(scaleToWidth, scaleToWidth);
    if (placeHeight > scaledImageHeight) {
        // there is maintain place bottom of image: center to vertical
        matrix.postTranslate(0, (placeHeight - scaledImageHeight) / 2);
    } else {
        // the bottom of image is already cropped
    }

    imageView.setImageMatrix(matrix);

imageView.getWidth() 你不会在 OnCreate 方法中得到正确的值,所以你需要使用 ViewTreeObserver,或者确切的大小。

于 2013-04-15T08:51:33.150 回答