Advertisement
Guest User

Untitled

a guest
May 24th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. public class AspectRatioImageView2 extends ImageView {
  2.  
  3. public AspectRatioImageView2(Context context)
  4. {
  5. super(context);
  6. }
  7.  
  8. public AspectRatioImageView2(Context context, AttributeSet attrs)
  9. {
  10. super(context, attrs);
  11. }
  12.  
  13. public AspectRatioImageView2(Context context, AttributeSet attrs,
  14. int defStyle)
  15. {
  16. super(context, attrs, defStyle);
  17. }
  18.  
  19. @Override
  20. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  21. {
  22. Drawable drawable = getDrawable();
  23. if (drawable != null)
  24. {
  25. int width = MeasureSpec.getSize(widthMeasureSpec);
  26. int diw = drawable.getIntrinsicWidth();
  27. if (diw > 0)
  28. {
  29. int height = width * drawable.getIntrinsicHeight() / diw;
  30. setMeasuredDimension(width, height);
  31. }
  32. else
  33. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  34. }
  35. else
  36. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement