Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class RatioFrameLayout extends FrameLayout {
  2.  
  3. private int widthRatio;
  4. private int heightRatio;
  5. private boolean widthStandards;
  6.  
  7.  
  8. public RatioFrameLayout(Context context) {
  9. super(context);
  10. }
  11.  
  12. public RatioFrameLayout(Context context, AttributeSet attrs) {
  13. super(context, attrs);
  14. TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioFrameLayout);
  15. widthRatio = typedArray.getInt(R.styleable.RatioFrameLayout_frame_width_ratio, 0);
  16. heightRatio = typedArray.getInt(R.styleable.RatioFrameLayout_frame_height_ratio, 0);
  17. widthStandards = typedArray.getBoolean(R.styleable.RatioFrameLayout_frame_width_standards, true);
  18.  
  19. }
  20.  
  21. @Override
  22. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  23. if (widthRatio != 0 && heightRatio != 0) {
  24. int width = MeasureSpec.getSize(widthMeasureSpec);
  25. int height = MeasureSpec.getSize(heightMeasureSpec);
  26. int splitValue;
  27. if (widthStandards) {
  28. splitValue = width / widthRatio;
  29. height = splitValue * heightRatio;
  30. } else {
  31. splitValue = height / heightRatio;
  32. width = splitValue * widthRatio;
  33. }
  34.  
  35. super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
  36. } else {
  37.  
  38. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement