LuckyJayce / LargeImage

Android 加载大图 可以高清显示10000*10000像素的图片,轻松实现微博长图功能
Apache License 2.0
2.33k stars 416 forks source link

设置合适的缩放大小应该能够显示全部图片到 view 中 #38

Open whuBobCat opened 6 years ago

whuBobCat commented 6 years ago

修改如下,当layout的宽大于高的时候,测试没有问题,使用largeImageView.setScale(largeImageView.getFitScale()); 设置图片缩放比例时,能够将图片完整显示

设置 0.9 的原因是:如果不设置,则会感觉图片占得太满,0.9 看起来刚刚好

    private void initFitImageScale(int imageWidth, int imageHeight) {
        final int layoutWidth = getMeasuredWidth();
        final int layoutHeight = getMeasuredHeight();
        if (imageWidth < imageHeight) {
            fitScale = 0.9f * imageWidth / imageHeight * layoutHeight / layoutWidth;
        } else {
            fitScale = 0.9f;
        }
        minScale = 0.25f;
        maxScale = 1.0f * imageWidth / layoutWidth;
        float a = (0.9f * imageWidth / layoutWidth) * layoutHeight / imageHeight;
        float density = getContext().getResources().getDisplayMetrics().density;
        maxScale = maxScale * density;
        if (maxScale < 4) {
            maxScale = 4;
        }
        if (minScale > a) {
            minScale = a;
        }
        if (criticalScaleValueHook != null) {
            minScale = criticalScaleValueHook.getMinScale(this, imageWidth, imageHeight, minScale);
            maxScale = criticalScaleValueHook.getMaxScale(this, imageWidth, imageHeight, maxScale);
        }
    }