Back to project page iwannawatch-android.
The source code is released under:
MIT License
If you think the Android project iwannawatch-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.meoyawn.iwannawatch; //from w ww.j a v a 2 s . c o m import android.content.Context; import android.util.AttributeSet; import android.widget.ImageView; import lombok.Getter; /** * An {@link android.widget.ImageView} layout that maintains a consistent width to height aspect ratio. */ public class DynamicHeightImageView extends ImageView { private double mHeightRatio; private @Getter int calculatedWidth; public DynamicHeightImageView(Context context, AttributeSet attrs) { super(context, attrs); } public DynamicHeightImageView(Context context) { super(context); } public void setHeightRatio(double ratio) { if (ratio != mHeightRatio) { mHeightRatio = ratio; requestLayout(); } } public double getHeightRatio() { return mHeightRatio; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mHeightRatio > 0.0) { // set the image views size calculatedWidth = MeasureSpec.getSize(widthMeasureSpec); int height = (int) (calculatedWidth * mHeightRatio); setMeasuredDimension(calculatedWidth, height); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } }