Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I like answer of arnefm but he made a small mistake (see comments) which I will try to correct:</p> <pre><code>import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; /** * ImageView that keeps aspect ratio when scaled */ public class ScaleImageView extends ImageView { public ScaleImageView(Context context) { super(context); } public ScaleImageView(Context context, AttributeSet attrs) { super(context, attrs); } public ScaleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { try { Drawable drawable = getDrawable(); if (drawable == null) { setMeasuredDimension(0, 0); } else { int measuredWidth = MeasureSpec.getSize(widthMeasureSpec); int measuredHeight = MeasureSpec.getSize(heightMeasureSpec); if (measuredHeight == 0 &amp;&amp; measuredWidth == 0) { //Height and width set to wrap_content setMeasuredDimension(measuredWidth, measuredHeight); } else if (measuredHeight == 0) { //Height set to wrap_content int width = measuredWidth; int height = width * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth(); setMeasuredDimension(width, height); } else if (measuredWidth == 0){ //Width set to wrap_content int height = measuredHeight; int width = height * drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight(); setMeasuredDimension(width, height); } else { //Width and height are explicitly set (either to match_parent or to exact value) setMeasuredDimension(measuredWidth, measuredHeight); } } } catch (Exception e) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } </code></pre> <p>Thus your <code>ImageView</code> will be scaled properly and will have no dimension problems if (for instance) put inside of <code>ScrollView</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload