7 package com.google.appinventor.components.runtime;
9 import android.content.Context;
10 import android.graphics.Canvas;
11 import android.graphics.Rect;
12 import android.os.Build.VERSION;
13 import android.os.Build.VERSION_CODES;
14 import android.util.AttributeSet;
15 import android.view.Gravity;
16 import android.view.MotionEvent;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.view.ViewParent;
32 private static final int MATRIX_SAVE_FLAG = 0x01;
35 private int mLeftWidth;
38 private int mRightWidth;
41 private final Rect mTmpContainerRect =
new Rect();
42 private final Rect mTmpChildRect =
new Rect();
44 private float mScale = 1.0f;
51 this(context, attrs, 0);
55 super(context, attrs, defStyle);
56 setClipChildren(
false);
62 canvas.scale(mScale, mScale);
63 super.dispatchDraw(canvas);
72 final int[] scaledLocation = { (int) (location[0] * mScale),
73 (int) (location[1] * mScale) };
75 final Rect scaledDirty =
new Rect((
int) (dirty.left * mScale),
76 (
int) (dirty.top * mScale), (
int) (dirty.right * mScale),
77 (
int) (dirty.bottom * mScale));
79 this.invalidate(scaledDirty);
81 return super.invalidateChildInParent(scaledLocation, scaledDirty);
87 ev.setLocation(ev.getX() * (1f / mScale), ev.getY() * (1f / mScale));
88 super.dispatchTouchEvent(ev);
94 updatePadding(getWidth(), getHeight());
97 private void updatePadding(
int width,
int height) {
100 int paddingRight = (int)((width * (mScale - 1f)) / mScale);
101 int paddingBottom = (int)((height * (mScale - 1f)) / mScale);
102 setPadding(0, 0, paddingRight, paddingBottom);
123 protected void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec) {
124 int count = getChildCount();
133 for (
int i = 0; i < count; i++) {
134 final View child = getChildAt(i);
135 if (child.getVisibility() != GONE) {
136 measureChild(child, widthMeasureSpec, heightMeasureSpec);
138 mLeftWidth += Math.max(maxWidth, child.getMeasuredWidth());
140 maxHeight = Math.max(maxHeight, child.getMeasuredHeight());
148 maxWidth += mLeftWidth + mRightWidth;
150 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
151 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
154 setMeasuredDimension(
159 setMeasuredDimension(
160 resolveSize(maxWidth, widthMeasureSpec),
161 resolveSize(maxHeight, heightMeasureSpec));
169 protected void onLayout(
boolean changed,
int left,
int top,
int right,
171 final int count = getChildCount();
173 int leftPos = getPaddingLeft();
175 final int parentTop = getPaddingTop();
176 final int parentBottom = bottom - top - getPaddingBottom();
178 for (
int i = 0; i < count; i++) {
179 final View child = getChildAt(i);
180 if (child.getVisibility() != GONE) {
181 final int width = child.getMeasuredWidth();
182 final int height = child.getMeasuredHeight();
184 mTmpContainerRect.left = leftPos;
185 mTmpContainerRect.right = leftPos;
186 leftPos = mTmpContainerRect.right;
187 mTmpContainerRect.top = parentTop;
188 mTmpContainerRect.bottom = parentBottom;
190 Gravity.apply(Gravity.TOP | Gravity.LEFT, width, height,
191 mTmpContainerRect, mTmpChildRect);
193 child.layout(mTmpChildRect.left, mTmpChildRect.top,
194 mTmpChildRect.right, mTmpChildRect.bottom);