美文网首页
Glide 采样率 inSampleSize 计算

Glide 采样率 inSampleSize 计算

作者: gczxbb | 来源:发表于2021-01-16 11:37 被阅读0次

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 策略,选择 内存。其他策略 都是图片质量。


任重而道远

相关文章

  • Glide 采样率 inSampleSize 计算

    DownsampleStrategy 策略 抽象类,Glide 提供以下 六种 策略。 默认 CenterOuts...

  • Bitmap知识点

    采样率压缩 根据ImageView的大小,通过设置inSampleSize采样率,加载压缩后的图片。如下: inS...

  • bitmap面试问题讲解

    一、recycle 二、LRU 三、计算inSampleSize 四、缩略图 五、三级缓存 网络缓存/本地缓存/内存缓存

  • inSampleSize优化

    废话不多说先贴上常用的方法吧 inSampleSize的默认值和最小值为1(当小于1时,解码器将该值当做1来处理)...

  • 写个简单的异步图片压缩保存的类

    思路:AsyncTask + inSampleSize + bitmap.compress由于inSampleSi...

  • 【PY】FFT小结(2:采样率Fs的选择)

    接上文 此外还要谈一下采样率Fs的选取。在我的数值计算中,采样率就是时间步Ts的倒数啦,一般是很大(比如我这里是2...

  • 采样率和量化精度

    ● 采样率采样率实际上是指当将声音储存至计算机中,必须经过一个录音转换的过程,转换些什么呢?就是把声音这种模拟信号...

  • 音频帧占用内存计算

    其实本身没有音频帧这个概念的,只不过我们为了方便描述和计算才提出这个概念计算规则:采样率 x 位宽 x 采样时间 ...

  • 音频概念

    采样率: 采样位数: 音频位速,也叫码率,或者比特率 PCM 编码格式 音频文件大小计算:

  • Glide(图片加载)

    Glide Glide使用文档 Glide 系列教程 玩转Glide的Target对象 Glide-内存缓存与磁盘...

网友评论

      本文标题:Glide 采样率 inSampleSize 计算

      本文链接:https://www.haomeiwen.com/subject/zpypaktx.html