Skip to content

Instantly share code, notes, and snippets.

@ataulm
Last active August 29, 2015 14:01
Show Gist options
  • Save ataulm/0f359f8ae836908dca50 to your computer and use it in GitHub Desktop.
Save ataulm/0f359f8ae836908dca50 to your computer and use it in GitHub Desktop.
look Antonio, no hands!
package com.ataulm.mijur.view;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MatchParentWidthImageView extends ImageView {
public MatchParentWidthImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MatchParentWidthImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else {
int desiredHeight = calculateDesiredHeight(drawable, widthMeasureSpec);
int desiredHeightMeasureSpec = MeasureSpec.makeMeasureSpec(desiredHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, desiredHeightMeasureSpec);
}
}
private int calculateDesiredHeight(Drawable drawable, int widthMeasureSpec) {
float width = drawable.getIntrinsicWidth();
float height = drawable.getIntrinsicHeight();
float aspectRatio = width / height;
int availableWidth = MeasureSpec.getSize(widthMeasureSpec);
return (int) (availableWidth / aspectRatio + 0.5f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment