Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement