Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement