Created
April 14, 2015 09:56
-
-
Save ishitcno1/21f2e61879057934589d to your computer and use it in GitHub Desktop.
android aspect ration imageview
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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