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;
默認(rèn) CenterOutside。
根據(jù)源圖片寬高畅涂,和目標(biāo)展示請求的寬高,計(jì)算一個(gè) 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);
}
計(jì)算 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);
}
計(jì)算 source 寬/高 和 request 寬/高 比例蜘澜,最小值 minIntegerFactor。
如果 minIntegerFactor 是0响疚,表示至少一項(xiàng) source 小于 requested鄙信,按照 1 等比返回。
source 都大時(shí)忿晕,minIntegerFactor 不是0装诡,是最接近比例。
Integer highestOneBit() 方法践盼,返回<=該值的一個(gè)2的冪次方數(shù)鸦采,(例,如果是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 一個(gè)2的冪次方數(shù) lesserOrEqualSampleSize吐限。
如果取冪時(shí)變小鲜侥,<< 翻倍操作,盡可能大诸典。最終 返回比依然是 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 表示選擇 向上提高采樣率 降低內(nèi)存使用率篡撵,或者更高的圖片質(zhì)量 。
QUALITY 和 MEMORY
只有 AtMost 策略豆挽,選擇 內(nèi)存育谬。其他策略 都是圖片質(zhì)量。
任重而道遠(yuǎn)