ImageView that maintains a given aspect ratio, for example 6:4 for a standard photograph.
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
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
public class AspectImageView extends ImageView { | |
private static final int DEFAULT_XRATIO = 1; | |
private static final int DEFAULT_YRATIO = 1; | |
private int xRatio = DEFAULT_XRATIO; | |
private int yRatio = DEFAULT_YRATIO; | |
public int getXRatio() { | |
return xRatio; | |
} | |
public void setXRatio(int xRatio) { | |
this.xRatio = xRatio; | |
} | |
public int getYRatio() { | |
return yRatio; | |
} | |
public void setYRatio(int yRatio) { | |
this.yRatio = yRatio; | |
} | |
public AspectImageView(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public AspectImageView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public AspectImageView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs, defStyle); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AspectImageView, 0, 0); | |
try { | |
xRatio = a.getInt(R.styleable.AspectImageView_xRatio, DEFAULT_XRATIO); | |
yRatio = a.getInt(R.styleable.AspectImageView_yRatio, DEFAULT_YRATIO); | |
} finally { | |
a.recycle(); | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
if(widthMode == MeasureSpec.EXACTLY && (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED)) { | |
setMeasuredDimension(widthSize, (int)((double)widthSize / xRatio * yRatio)); | |
} else if(heightMode == MeasureSpec.EXACTLY && (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED)) { | |
setMeasuredDimension((int)((double)heightSize / yRatio * xRatio), heightSize); | |
} else { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
} | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="AspectImageView"> | |
<attr name="xRatio" format="integer"/> | |
<attr name="yRatio" format="integer"/> | |
</declare-styleable> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment