Android 이미지 해상도별 자동 비율 구하기
2020. 6. 29. 09:31ㆍAndroid
반응형
private ArrayList<Integer> maxWidth_maxHeight(Context context,Uri mSourceUri){
ArrayList<Integer> ho = new ArrayList<>();
BitmapFactory.Options options = new BitmapFactory.Options(); //비율 구하기
options.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(
context.getContentResolver().openInputStream(mSourceUri),
null,
options);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
float maxHeight = 0;
float maxWidth = 0;
float minRatio = 3.0f / 4.0f; //3:4 자신이 하고싶은 비율 넣기
float maxRatio = 16.0f / 9.0f; // 16:9
float ratio = (float)imageWidth / (float)imageHeight;
if (ratio < minRatio) {
maxWidth = (float)imageWidth;
maxHeight = (float)imageWidth / minRatio;
} else if (ratio > maxRatio) {
maxWidth = (float)imageHeight * maxRatio;
maxHeight = (float)imageHeight;
} else {
maxWidth = (float)imageWidth;
maxHeight = (float)imageHeight;
}
ho.add((int)maxWidth);
ho.add((int)maxHeight);
return ho;
}
반응형
'Android' 카테고리의 다른 글
Android Paging3 , Room , Flow 관련 백그라운드 쓰레드 (0) | 2022.05.04 |
---|---|
Android 크롭 기능 없이 이미지 자르기 (0) | 2020.06.29 |
[안드로이드] 각 프래그먼트 별 뒤로가기 버튼(백버튼) 이벤트 (뷰페이저 사용) (0) | 2020.06.15 |
Android ViewPager2 + TabView JAVA (0) | 2020.06.11 |
Android Recyclerview Checkbox position Check 유지 (0) | 2020.06.08 |