Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this one :</p> <pre><code>import android.content.Context; import android.graphics.Matrix; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.ViewGroup; public class CustomImageView extends android.widget.ImageView { public CustomImageView(Context context) { super(context); } public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); } public void fitYUniformly() { final Drawable drawable = getDrawable(); if (drawable == null) return; final int dwidth = drawable.getIntrinsicWidth(); final int dheight = drawable.getIntrinsicHeight(); if (dwidth == -1 || dheight == -1) return; int vheight = this.getHeight(); float scale = (float) vheight / (float) dheight; final int vwidth = (int) (dwidth * scale); scale(scale, vwidth, vheight); } public void fitXUniformly(int parentWidth) { final Drawable drawable = getDrawable(); if (drawable == null) return; final int dwidth = drawable.getIntrinsicWidth(); final int dheight = drawable.getIntrinsicHeight(); if (dwidth == -1 || dheight == -1) return; int vwidth = parentWidth;// here,you need to pass the width of parentview // int vwidth = this.getWidth(); float scale = (float) vwidth / (float) dwidth; final int vheight = (int) (dheight * scale); scale(scale, vwidth, vheight); } private void scale(float scale, int newWidth, int newHeight) { final ViewGroup.LayoutParams params = this.getLayoutParams(); params.width = newWidth; params.height = newHeight; this.setLayoutParams(params); this.setScaleType(ScaleType.MATRIX); final Matrix matrix = new Matrix(); matrix.setScale(scale, scale); this.setImageMatrix(matrix); } } </code></pre> <p><strong>Note:</strong></p> <p>Don't forget to call <code>fitXUniformly(parentWidth);</code> . here, <code>parentWidth</code> will be the width of <code>CustomImageView</code>'s parent.</p> <p>I hope it will be helpful !!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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