Index: talk/examples/android/src/org/appspot/apprtc/PercentFrameLayout.java |
diff --git a/talk/examples/android/src/org/appspot/apprtc/PercentFrameLayout.java b/talk/examples/android/src/org/appspot/apprtc/PercentFrameLayout.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2b916d437a9bbf0bf320fb2c5057780de57ba674 |
--- /dev/null |
+++ b/talk/examples/android/src/org/appspot/apprtc/PercentFrameLayout.java |
@@ -0,0 +1,112 @@ |
+/* |
+ * libjingle |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are met: |
+ * |
+ * 1. Redistributions of source code must retain the above copyright notice, |
+ * this list of conditions and the following disclaimer. |
+ * 2. Redistributions in binary form must reproduce the above copyright notice, |
+ * this list of conditions and the following disclaimer in the documentation |
+ * and/or other materials provided with the distribution. |
+ * 3. The name of the author may not be used to endorse or promote products |
+ * derived from this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+package org.appspot.apprtc; |
+ |
+import android.content.Context; |
+import android.util.AttributeSet; |
+import android.view.View; |
+import android.view.ViewGroup; |
+ |
+/** |
+ * Simple container that confines the children to a subrectangle specified as percentage values of |
+ * the container size. The children are centered horizontally and vertically inside the confined |
+ * space. |
+ */ |
+public class PercentFrameLayout extends ViewGroup { |
+ private int xPercent = 0; |
+ private int yPercent = 0; |
+ private int widthPercent = 100; |
+ private int heightPercent = 100; |
+ |
+ public PercentFrameLayout(Context context) { |
+ super(context); |
+ } |
+ |
+ public PercentFrameLayout(Context context, AttributeSet attrs) { |
+ super(context, attrs); |
+ } |
+ |
+ public PercentFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
+ super(context, attrs, defStyleAttr); |
+ } |
+ |
+ public void setPosition(int xPercent, int yPercent, int widthPercent, int heightPercent) { |
+ this.xPercent = xPercent; |
+ this.yPercent = yPercent; |
+ this.widthPercent = widthPercent; |
+ this.heightPercent = heightPercent; |
+ } |
+ |
+ @Override |
+ public boolean shouldDelayChildPressedState() { |
+ return false; |
+ } |
+ |
+ @Override |
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
+ final int width = getDefaultSize(Integer.MAX_VALUE, widthMeasureSpec); |
+ final int height = getDefaultSize(Integer.MAX_VALUE, heightMeasureSpec); |
+ setMeasuredDimension( |
+ MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), |
+ MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
+ |
+ final int childWidthMeasureSpec = |
+ MeasureSpec.makeMeasureSpec(width * widthPercent / 100, MeasureSpec.AT_MOST); |
+ final int childHeightMeasureSpec = |
+ MeasureSpec.makeMeasureSpec(height * heightPercent / 100, MeasureSpec.AT_MOST); |
+ for (int i = 0; i < getChildCount(); ++i) { |
+ final View child = getChildAt(i); |
+ if (child.getVisibility() != GONE) { |
+ child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
+ } |
+ } |
+ } |
+ |
+ @Override |
+ protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
+ final int width = right - left; |
+ final int height = bottom - top; |
+ // Sub-rectangle specified by percentage values. |
+ final int subWidth = width * widthPercent / 100; |
+ final int subHeight = height * heightPercent / 100; |
+ final int subLeft = left + width * xPercent / 100; |
+ final int subTop = top + height * yPercent / 100; |
+ |
+ for (int i = 0; i < getChildCount(); ++i) { |
+ final View child = getChildAt(i); |
+ if (child.getVisibility() != GONE) { |
+ final int childWidth = child.getMeasuredWidth(); |
+ final int childHeight = child.getMeasuredHeight(); |
+ // Center child both vertically and horizontally. |
+ final int childLeft = subLeft + (subWidth - childWidth) / 2; |
+ final int childTop = subTop + (subHeight - childHeight) / 2; |
+ child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight); |
+ } |
+ } |
+ } |
+} |