Skip to content

Instantly share code, notes, and snippets.

@raviteja83
Created August 14, 2016 18:34
Show Gist options
  • Save raviteja83/09678b7f086b05e7eb6ecdf0f8eb00d4 to your computer and use it in GitHub Desktop.
Save raviteja83/09678b7f086b05e7eb6ecdf0f8eb00d4 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
/**
* Created by Ravi-PC on 04-12-2015.
**/
public class AspectImageView extends ImageView {
public AspectImageView(Context context) {
this(context,null);
}
public AspectImageView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public AspectImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//Figure out the aspect ratio of the image content
int desiredSize;
float aspect;
Drawable d = getDrawable();
if (d == null) {
desiredSize = 0;
aspect = 1f;
} else {
desiredSize = d.getIntrinsicWidth();
aspect = (float) d.getIntrinsicWidth() / (float) d.getIntrinsicHeight();
}
//Get the width based on the measure specs
int widthSize = View.resolveSize(desiredSize, widthMeasureSpec);
//Calculate height based on aspect
int heightSize = (int)(widthSize / aspect);
//Make sure the height we want is not too large
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);
if (specMode == MeasureSpec.AT_MOST || specMode == MeasureSpec.EXACTLY) {
//If our measurement exceeds the max height, shrink back
if (heightSize > specSize) {
heightSize = specSize;
widthSize = (int)(heightSize * aspect);
}
}
//MUST do this to store the measurements
setMeasuredDimension(widthSize, heightSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment