Skip to content

Commit 6a8e0fb

Browse files
author
fengjian
committed
add $inSampleSize to BitmapLess for calculate the best inSampleSize by
reqWidth and reqHeight
1 parent 45fe95f commit 6a8e0fb

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,30 @@
11
package com.jayfeng.lesscode.core;
22

3+
import android.graphics.BitmapFactory;
4+
35
public final class BitmapLess {
6+
7+
/**
8+
* 根据reqWidth, reqHeight计算最合适的inSampleSize
9+
* @param options
10+
* @param reqWidth
11+
* @param reqHeight
12+
* @return
13+
*/
14+
public static int $inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
15+
// raw height and width of image
16+
int rawWidth = options.outWidth;
17+
int rawHeight = options.outHeight;
18+
19+
// calculate best sample size
20+
int inSampleSize = 0;
21+
if (rawHeight > reqHeight || rawWidth > reqWidth) {
22+
float ratioWidth = (float) rawWidth / reqWidth;
23+
float ratioHeight = (float) rawHeight / reqHeight;
24+
inSampleSize = (int) Math.min(ratioHeight, ratioWidth);
25+
}
26+
inSampleSize = Math.max(1, inSampleSize);
27+
28+
return inSampleSize;
29+
}
430
}

0 commit comments

Comments
 (0)