SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DynamicHeightImageView extends ImageView {
  2.  
  3.     private double mHeightRatio;
  4.  
  5.     public DynamicHeightImageView(Context context, AttributeSet attrs) {
  6.         super(context, attrs);
  7.     }
  8.  
  9.     public DynamicHeightImageView(Context context) {
  10.         super(context);
  11.     }
  12.  
  13.     public void setHeightRatio(double ratio) {
  14.         if (ratio != mHeightRatio) {
  15.             mHeightRatio = ratio;
  16.             requestLayout();
  17.         }
  18.     }
  19.  
  20.     public double getHeightRatio() {
  21.         return mHeightRatio;
  22.     }
  23.  
  24.     @Override
  25.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  26.         if (mHeightRatio > 0.0) {
  27.             int width = MeasureSpec.getSize(widthMeasureSpec);
  28.             int height = (int) (width * mHeightRatio);
  29.             setMeasuredDimension(width, height);
  30.         }
  31.         else {
  32.             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  33.         }
  34.     }
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top