Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Android ImageView for top-crop scaling of the contained drawable.
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Created by chris on 7/27/16.
*/
public class TopCropImageView extends ImageView {
public TopCropImageView(Context context) {
super(context);
init();
}
public TopCropImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
recomputeImgMatrix();
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
recomputeImgMatrix();
return super.setFrame(l, t, r, b);
}
private void init() {
setScaleType(ScaleType.MATRIX);
}
private void recomputeImgMatrix() {
final Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
final Matrix matrix = getImageMatrix();
float scale;
final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
final int drawableWidth = drawable.getIntrinsicWidth();
final int drawableHeight = drawable.getIntrinsicHeight();
if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
scale = (float) viewHeight / (float) drawableHeight;
} else {
scale = (float) viewWidth / (float) drawableWidth;
}
matrix.setScale(scale, scale);
setImageMatrix(matrix);
}
}
@Wavesonics

This comment has been minimized.

Copy link

Wavesonics commented Nov 8, 2012

Great snippet, works perfectly!

@suda

This comment has been minimized.

Copy link

suda commented Jan 14, 2013

Looks like it's missing

import android.content.Context;

import :)

@arriolac

This comment has been minimized.

Copy link
Owner Author

arriolac commented Jan 18, 2013

@suda, thanks!

@idnael

This comment has been minimized.

Copy link

idnael commented May 17, 2013

your soluton worked for me until I change the device orientation!
Then setFrame is called with the previous view width value.
SO I think maybe I should put the change matrix code in another method?

@gbenson-nflx

This comment has been minimized.

Copy link

gbenson-nflx commented May 20, 2014

This was helpful but I also had to override onLayout() to get the scaling matrix to update properly for layout changes. Using setFrame() was not enough given my usage.

e.g.:

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        recomputeImgMatrix();
    }
@ErNaveen

This comment has been minimized.

Copy link

ErNaveen commented Aug 13, 2014

need imageview with zoom in and out with croping can i use it

@followztx

This comment has been minimized.

Copy link

followztx commented Dec 30, 2014

@gbenson-nflx thanks .

@enknamel

This comment has been minimized.

Copy link

enknamel commented Feb 2, 2015

@gbenson-nflx My images were not showing up at all until I added your code. Very strange. Though I am using this as a background for a layout in a scrollview so something wonky is going on.

@cesards

This comment has been minimized.

Copy link

cesards commented Feb 17, 2015

I developed a library for this purpose. I think it's a litte bit late but you could add the dependency and replace it ;-)

https://github.com/cesards/CropImageView

Have fun!!

@arriolac

This comment has been minimized.

Copy link
Owner Author

arriolac commented Mar 11, 2015

@gbenson-nflx updated the gist from the snippet you shared. Thanks!

@zelin

This comment has been minimized.

Copy link

zelin commented Apr 8, 2015

Great snippet ^^

@adriancosma

This comment has been minimized.

Copy link

adriancosma commented May 2, 2015

I would like to have the imageview crop the top center part of the image, now seems that is top left. How can I change the code to obtain a crop centered and not left aligned? Thanks!

@briandilley

This comment has been minimized.

Copy link

briandilley commented Jun 24, 2015

to center it:

    private void recomputeImgMatrix() {
        final Matrix matrix = getImageMatrix();

        float scale;
        final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        final int drawableWidth = getDrawable().getIntrinsicWidth();
        final int drawableHeight = getDrawable().getIntrinsicHeight();

        if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
            scale = (float) viewHeight / (float) drawableHeight;
        } else {
            scale = (float) viewWidth / (float) drawableWidth;
        }

        matrix.setScale(scale, scale);
        if ((drawableWidth * scale) > viewWidth) {
            float tr = -(((drawableWidth * scale) - viewWidth) / 2);
            matrix.postTranslate(tr, 0);
        }
        setImageMatrix(matrix);
    }
@Bapho

This comment has been minimized.

Copy link

Bapho commented Aug 11, 2015

be aware that the drawable can be null in recomputeImgMatrix().

@omidMirrajei

This comment has been minimized.

Copy link

omidMirrajei commented Aug 11, 2015

hi:
i import this class to my project ...
how to use for another class and use for image view ?

@omidMirrajei

This comment has been minimized.

Copy link

omidMirrajei commented Aug 12, 2015

pls any tell me

@spartacus777

This comment has been minimized.

Copy link

spartacus777 commented Sep 7, 2015

Worked perfectly in conjunction with UIL (universal image loader)

@cesards

This comment has been minimized.

Copy link

cesards commented Oct 20, 2015

This snippet has a bug in devices with API < 16

I recommend use my lib. It`s already fixed:

here

@nv95

This comment has been minimized.

Copy link

nv95 commented Oct 23, 2015

Thanks! But it's will be better for using in xml with additional constructors:

    public TopCropImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setScaleType(ScaleType.MATRIX);
    }

    public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setScaleType(ScaleType.MATRIX);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setScaleType(ScaleType.MATRIX);
    }
@MrVilkaman

This comment has been minimized.

Copy link

MrVilkaman commented Feb 2, 2016

to left side add matrix.postTranslate(viewWidth - drawableWidth*scale,0); after matrix.setScale(scale, scale);

@naeimrezaeian

This comment has been minimized.

Copy link

naeimrezaeian commented Apr 9, 2016

when i use this class compile show this error
android.view.InflateException: Binary XML file

@jrh4016

This comment has been minimized.

Copy link

jrh4016 commented Apr 14, 2016

I'd love to get an explanation on how to use this in detail.

I've created a new file with this name and copied over the code. I then changed my ImageView to a TopCropImageView in XML, but I'm getting a litany of errors
Custom view TopCropImageView is not using the 2- or 3-argument View constructors; XML attributes will not work
java.lang.NullPointerException at line 35
Unable to start activity... android.view.InflateException: Binary XML file line #62: Binary XML file line #62: Error inflating class

@Firsto

This comment has been minimized.

Copy link

Firsto commented Apr 18, 2016

add this code

@barakhadar

This comment has been minimized.

Copy link

barakhadar commented Apr 19, 2016

First, great stuff! It works great! But, there's a but.. What I'm looking for is exactly the opposite...
How do I modify the code so that I'll have the same effect, but with the bottom of my image rather that it's top (as it works by default) ?

@barakhadar

This comment has been minimized.

Copy link

barakhadar commented Apr 19, 2016

Never mind... Thanks to @MrVilkaman answer, I found the way.. Thanks dude!

So what you'll do to accomplish that, simply add at the bottom of the class, right after matrix.setScale(scale, scale);, this line : matrix.postTranslate( 0, viewHeight - drawableHeight * scale);.

Hope it will help someone else too..
Cheers!

@arriolac

This comment has been minimized.

Copy link
Owner Author

arriolac commented Jul 26, 2016

It's been a while since I've updated this class which explains why there's some issues/warnings caused by newer APIs. I'll be posting an update to this code soon.

@arriolac

This comment has been minimized.

Copy link
Owner Author

arriolac commented Jul 27, 2016

Updated gist to now work when inflated in XML and making sure that the drawable is not null before using it.

@seyoung-hyun

This comment has been minimized.

Copy link

seyoung-hyun commented Feb 12, 2018

@arriolac
Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.

@klemenzarn

This comment has been minimized.

Copy link

klemenzarn commented Apr 11, 2018

Thanks for this.

@lannyf77

This comment has been minimized.

Copy link

lannyf77 commented Jun 30, 2018

final Matrix matrix = getImageMatrix();

any concern about the doc says "Do not change this matrix in place but make a copy.":

/** Returns the view's optional matrix. This is applied to the
view's drawable when it is drawn. If there is no matrix,
this method will return an identity matrix.
Do not change this matrix in place but make a copy.
If you want a different matrix applied to the drawable,
be sure to call setImageMatrix().
*/
public Matrix getImageMatrix() {
if (mDrawMatrix == null) {
return new Matrix(Matrix.IDENTITY_MATRIX);
}
return mDrawMatrix;
}

@Hitesh309

This comment has been minimized.

Copy link

Hitesh309 commented Jan 10, 2019

Image not loading after swipe to refresh layout in glide library.
Please give some feedback.
`public class TopCropImageView extends android.support.v7.widget.AppCompatImageView {

public TopCropImageView(Context context) {
    super(context);
    init();
}

public TopCropImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TopCropImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr);
    init();
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    recomputeImgMatrix();
}

@Override
protected boolean setFrame(int l, int t, int r, int b) {
    recomputeImgMatrix();
    return super.setFrame(l, t, r, b);
}


private void init() {
    setScaleType(ScaleType.MATRIX);
}

private void recomputeImgMatrix() {

    Drawable drawable = getDrawable();
    if (drawable != null) {

        final Matrix matrix = getImageMatrix();

        float scale;
        final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
        final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
        final int drawableWidth = getDrawable().getIntrinsicWidth();
        final int drawableHeight = getDrawable().getIntrinsicHeight();

        if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
            scale = (float) viewHeight / (float) drawableHeight;
        } else {
            scale = (float) viewWidth / (float) drawableWidth;
        }
        matrix.setScale(scale, scale);

        if ((drawableWidth * scale) > viewWidth) {
            float tr = -(((drawableWidth * scale) - viewWidth) / 2);
            matrix.postTranslate(tr, 0);
        }
        setImageMatrix(matrix);
    }
}

}
`

@Dishant624

This comment has been minimized.

Copy link

Dishant624 commented Jan 10, 2019

please add this method on code
@Override protected void onDraw(Canvas canvas) { recomputeImgMatrix(); super.onDraw(canvas); }

@Hitesh309

This comment has been minimized.

Copy link

Hitesh309 commented Jan 10, 2019

Thanks @Dishant624 for your code
You save my day :)

@MichalDanielDobrzanski

This comment has been minimized.

Copy link

MichalDanielDobrzanski commented Jul 2, 2019

@Dishant624 - could you elaborate why? It would be much more costly in terms of performance.
Also this was enough for me:

@Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        recomputeImgMatrix();
    }

Hence no need for overriding setFrame method. Also, setFrame is supported ONLY for two View subclasses: https://stackoverflow.com/questions/4751963/android-why-cant-i-override-setframe-from-view

In Kotlin, I got succeeded with MotionLayout with:

class BottomCenterImageView : AppCompatImageView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    init {
        scaleType = ScaleType.MATRIX
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        recomputeImageMatrix()
    }

    private fun recomputeImageMatrix() {
        val drawable = drawable ?: return
        val viewWidth = width - paddingLeft - paddingRight
        val viewHeight = height - paddingTop - paddingBottom
        val drawableWidth = drawable.intrinsicWidth
        val drawableHeight = drawable.intrinsicHeight
        imageMatrix = imageMatrix.apply {
            setTranslate(
                Math.round((viewWidth - drawableWidth) * 0.5f).toFloat(),
                Math.round((viewHeight - drawableHeight).toFloat()).toFloat()
            )
        }
    }
}
@nodermann

This comment has been minimized.

Copy link

nodermann commented Nov 17, 2019

Hi, how I can change it to crop at the bottom?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.