Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RatioFrameLayout extends FrameLayout {
- private int widthRatio;
- private int heightRatio;
- private boolean widthStandards;
- public RatioFrameLayout(Context context) {
- super(context);
- }
- public RatioFrameLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioFrameLayout);
- widthRatio = typedArray.getInt(R.styleable.RatioFrameLayout_frame_width_ratio, 0);
- heightRatio = typedArray.getInt(R.styleable.RatioFrameLayout_frame_height_ratio, 0);
- widthStandards = typedArray.getBoolean(R.styleable.RatioFrameLayout_frame_width_standards, true);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- if (widthRatio != 0 && heightRatio != 0) {
- int width = MeasureSpec.getSize(widthMeasureSpec);
- int height = MeasureSpec.getSize(heightMeasureSpec);
- int splitValue;
- if (widthStandards) {
- splitValue = width / widthRatio;
- height = splitValue * heightRatio;
- } else {
- splitValue = height / heightRatio;
- width = splitValue * widthRatio;
- }
- super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
- } else {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement