Android Open Source - tdpforce Dynamic Height Image View

From Project

Back to project page tdpforce.


The source code is released under:

Apache License

If you think the Android project tdpforce listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* w  w w .j a v  a 2s  . co  m*/
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

 * An {@link android.widget.ImageView} layout that maintains a consistent width to height aspect ratio.
public class DynamicHeightImageView extends ImageView {

    private double mHeightRatio;

    public DynamicHeightImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

    public DynamicHeightImageView(Context context) {

    public void setHeightRatio(double ratio) {
        if (ratio != mHeightRatio) {
            mHeightRatio = ratio;

    public double getHeightRatio() {
        return mHeightRatio;

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mHeightRatio > 0.0) {
            // set the image views size
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = (int) (width * mHeightRatio);
            setMeasuredDimension(width, height);
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);

Java Source Code List