public class ScreenWidthImgView extends ImageView {
public ScreenWidthImgView(Context context) {
super(context);
setScaleType(ScaleType.FIT_XY);
}
public ScreenWidthImgView(Context context, AttributeSet attrs) {
super(context, attrs);
setScaleType(ScaleType.FIT_XY);
}
public ScreenWidthImgView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setScaleType(ScaleType.FIT_XY);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getDrawable();
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * d.getIntrinsicHeight() / d.getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
Using this ImageView
with layout_width="match_parent"
and layout_height="wrap_content"
will give you an image that always takes up the full width of the screen and scales the image uniformly.