반응형

셋팅해야 되는 이미지의 사이즈가 일정하지 않을 경우 변경될 Width의 비율에 맞춰 Height를 수정하면 원본 비율에 맞는 이미지로 변경할 수 있습니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static Bitmap setRatioImage(Context context, ImageView view, Bitmap src, int imageId) {
    Bitmap bitmap;
    try {
        int viewWidth = view.getWidth();
        int srcWidth = src.getWidth();
       
        float ratio = (float)viewWidth / (float)srcWidth;
       
        float height = src.getHeight() * ratio;
        bitmap = Bitmap.createScaledBitmap(src, (int)viewWidth, (int)height, false);
    } catch (Exception e) {
        // TODO: handle exception
        bitmap = BitmapFactory.decodeResource(context.getResources(), imageId);
    }
    return bitmap;
}
cs

 

반응형

+ Recent posts