如何在SubsamplingScaleImageView中创建图像适合宽度(davemorrissey lib)

时间:2017-08-31 12:10:24

标签: android

当前代码: -

setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)

适用于匹配最短的图像尺寸和

setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE)

适用于匹配最宽的尺寸,似乎没有任何方式专门匹配宽度或高度?

2 个答案:

答案 0 :(得分:1)

我的匹配宽度的方法如下

public void instantiateItem() {
    ...
    _ssiImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
    _ssiImageView.setMinScale(getFitWidthScale());
    ...
}

private float getFitWidthScale(){
    float windowWidth = App.getWindowWidth(); //Acquires window's pixel width
    return = windowWidth/getImageWidth();
}

private float getImageWidth(){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(_file.getAbsolutePath(), options);
    return options.outWidth;
}

如果SubsamplingScaleImageView填充整个屏幕宽度,这只能正常工作。基本上窗口宽度和视图宽度需要相同。但是,如果你可以获得SubsamplingScaleImageView的像素宽度,你可以使用它而不是windowWidth。

答案 1 :(得分:0)

您可以使用setImage(ImageSource imageSource, ImageViewState state)这样的方法:

ssiImageView.setImage(ImageSource.uri(Uri.fromFile(new File(imgFilePath))), new ImageViewState(getFitWidthScale(), new PointF(0, 0), SubsamplingScaleImageView.ORIENTATION_USE_EXIF));

private float getFitWidthScale(){
    float windowWidth = App.getWindowWidth(); //Acquires window's pixel width
    return = windowWidth/getImageWidth();
}

private float getImageWidth(){
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(_file.getAbsolutePath(), options);
    return options.outWidth;
}