Glide 采样率 inSampleSize 计算

2021-01-16  本文已影响0人  gczxbb

DownsampleStrategy 策略 抽象类,Glide 提供以下 六种 策略。

public static final DownsampleStrategy FIT_CENTER = new FitCenter();
public static final DownsampleStrategy CENTER_OUTSIDE = new CenterOutside();
public static final DownsampleStrategy AT_LEAST = new AtLeast();
public static final DownsampleStrategy AT_MOST = new AtMost();
public static final DownsampleStrategy CENTER_INSIDE = new CenterInside();

public static final DownsampleStrategy NONE = new None();

public static final DownsampleStrategy DEFAULT = CENTER_OUTSIDE;

默认 CenterOutside。
根据源图片宽高,和目标展示请求的宽高,计算一个 scale 比例。

FitCenter 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    float widthPercentage = requestedWidth / (float) sourceWidth;
    float heightPercentage = requestedHeight / (float) sourceHeight;
    return Math.min(widthPercentage, heightPercentage);
}

计算 requested 宽/高和 source 宽/高的比例,最小值选择。
如果 requested 都小,按照最大 scale 选择 采样率。

CenterOutside 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    float widthPercentage = requestedWidth / (float) sourceWidth;
    float heightPercentage = requestedHeight / (float) sourceHeight;
    return Math.max(widthPercentage, heightPercentage);
}

和 FIT_CENTER 相反。
如果 requested 都小,返回值<1。按照最小 scale 选择 采样率,即 source 和 request 比例最接近的。

AtLeast 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    int minIntegerFactor = Math.min(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
    return minIntegerFactor == 0 ? 1f : 1f / Integer.highestOneBit(minIntegerFactor);
}

计算 source 宽/高 和 request 宽/高 比例,最小值 minIntegerFactor。

如果 minIntegerFactor 是0,表示至少一项 source 小于 requested,按照 1 等比返回。
source 都大时,minIntegerFactor 不是0,是最接近比例。
Integer highestOneBit() 方法,返回<=该值的一个2的幂次方数,(例,如果是3,则返回2。),最终 返回比依然是 request / source。

AtMost 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    int maxIntegerFactor = (int) Math.ceil(Math.max(sourceHeight / (float) requestedHeight,
              sourceWidth / (float) requestedWidth));
    int lesserOrEqualSampleSize = Math.max(1, Integer.highestOneBit(maxIntegerFactor));
    int greaterOrEqualSampleSize =
          lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0);
    return 1f / greaterOrEqualSampleSize;
}

source 宽/高 和 request 宽/高 比例,选择最大值,向上取整 maxIntegerFactor。
返回<= maxIntegerFactor 一个2的幂次方数 lesserOrEqualSampleSize。
如果取幂时变小,<< 翻倍操作,尽可能大。最终 返回比依然是 request / source。

CenterInside 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    return Math.min(1.f,
          FIT_CENTER.getScaleFactor(sourceWidth, sourceHeight, requestedWidth, requestedHeight));
}

依赖 FIT_CENTER。
如果 FIT_CENTER 的 min 值 >1,说明 requested 宽高都大的情况,按照 1等比返回。
否则,按照 FIT_CENTER 值返回。

None 策略

@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
        int requestedHeight) {
    return 1f;
}

不做处理,以 1f 等比返回。

SampleSizeRounding 表示选择 向上提高采样率 降低内存使用率,或者更高的图片质量 。
QUALITY 和 MEMORY

只有 AtMost 策略,选择 内存。其他策略 都是图片质量。


任重而道远

上一篇 下一篇

猜你喜欢

热点阅读