Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Created April 14, 2015 09:56
Show Gist options
  • Save ishitcno1/21f2e61879057934589d to your computer and use it in GitHub Desktop.
Save ishitcno1/21f2e61879057934589d to your computer and use it in GitHub Desktop.
android aspect ration imageview
public class AspectRatioImageView2 extends ImageView {
public AspectRatioImageView2(Context context)
{
super(context);
}
public AspectRatioImageView2(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AspectRatioImageView2(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
Drawable drawable = getDrawable();
if (drawable != null)
{
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
if (diw > 0)
{
int height = width * drawable.getIntrinsicHeight() / diw;
setMeasuredDimension(width, height);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment