1 /* 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.appspot.apprtc; 12 13 import android.content.Context; 14 import android.util.AttributeSet; 15 import android.view.View; 16 import android.view.ViewGroup; 17 18 /** 19 * Simple container that confines the children to a subrectangle specified as percentage values of 20 * the container size. The children are centered horizontally and vertically inside the confined 21 * space. 22 */ 23 public class PercentFrameLayout extends ViewGroup { 24 private int xPercent = 0; 25 private int yPercent = 0; 26 private int widthPercent = 100; 27 private int heightPercent = 100; 28 29 public PercentFrameLayout(Context context) { 30 super(context); 31 } 32 33 public PercentFrameLayout(Context context, AttributeSet attrs) { 34 super(context, attrs); 35 } 36 37 public PercentFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 38 super(context, attrs, defStyleAttr); 39 } 40 41 public void setPosition(int xPercent, int yPercent, int widthPercent, int heightPercent) { 42 this.xPercent = xPercent; 43 this.yPercent = yPercent; 44 this.widthPercent = widthPercent; 45 this.heightPercent = heightPercent; 46 } 47 48 @Override 49 public boolean shouldDelayChildPressedState() { 50 return false; 51 } 52 53 @Override 54 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 55 final int width = getDefaultSize(Integer.MAX_VALUE, widthMeasureSpec); 56 final int height = getDefaultSize(Integer.MAX_VALUE, heightMeasureSpec); 57 setMeasuredDimension( 58 MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), 59 MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 60 61 final int childWidthMeasureSpec = 62 MeasureSpec.makeMeasureSpec(width * widthPercent / 100, MeasureSpec.AT_MOST); 63 final int childHeightMeasureSpec = 64 MeasureSpec.makeMeasureSpec(height * heightPercent / 100, MeasureSpec.AT_MOST); 65 for (int i = 0; i < getChildCount(); ++i) { 66 final View child = getChildAt(i); 67 if (child.getVisibility() != GONE) { 68 child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 69 } 70 } 71 } 72 73 @Override 74 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 75 final int width = right - left; 76 final int height = bottom - top; 77 // Sub-rectangle specified by percentage values. 78 final int subWidth = width * widthPercent / 100; 79 final int subHeight = height * heightPercent / 100; 80 final int subLeft = left + width * xPercent / 100; 81 final int subTop = top + height * yPercent / 100; 82 83 for (int i = 0; i < getChildCount(); ++i) { 84 final View child = getChildAt(i); 85 if (child.getVisibility() != GONE) { 86 final int childWidth = child.getMeasuredWidth(); 87 final int childHeight = child.getMeasuredHeight(); 88 // Center child both vertically and horizontally. 89 final int childLeft = subLeft + (subWidth - childWidth) / 2; 90 final int childTop = subTop + (subHeight - childHeight) / 2; 91 child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); 92 } 93 } 94 } 95 } 96