나의 플랫폼/안드로이드
[Android] ResizableImageView
GsBOB
2015. 12. 29. 14:28
ImageView 에서 width를 화면으로 가득 채우고, 해당 Image에 따라 Height를 조절하고자 할 경우,
ImageView의 scaleType만으론 표현이 불가능하다.
이럴 경우 아래와 같이 ImageView를 커스텀화 해서 사용하자.
public class ResizableImageView extends ImageView {
public ResizableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
Drawable d = getDrawable();
if(d!=null){
// ceil not round - avoid thin vertical gaps along the left/right edges
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
setMeasuredDimension(width, height);
}else{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
출처 : http://stackoverflow.com/a/12283909/3534559
위 width 상태에 맞춰 height를 구하여 설정하는 커스텀 ImageView 이다.
참고하세요.