Skip to content

Instantly share code, notes, and snippets.

@markus2610
Forked from arriolac/TopCropImageView.java
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save markus2610/6ba0f8866e6232b7c6e8 to your computer and use it in GitHub Desktop.

Select an option

Save markus2610/6ba0f8866e6232b7c6e8 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Matrix;
import android.widget.ImageView;
/**
* ImageView to display top-crop scale of an image view.
*
* @author Chris Arriola
*/
public class TopCropImageView extends ImageView {
public TopCropImageView(Context context) {
super(context);
setScaleType(ScaleType.MATRIX);
}
@Override
protected boolean setFrame(int l, int t, int r, int b) {
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);
setImageMatrix(matrix);
return super.setFrame(l, t, r, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment