Skip to content

Commit b2b1571

Browse files
Philip MilneAndroid (Google) Code Review
authored andcommitted
Merge "Promote layout debugging code from GridLayout to ViewGroup."
2 parents 0b7d747 + 10ca24a commit b2b1571

File tree

4 files changed

+131
-56
lines changed

4 files changed

+131
-56
lines changed

core/java/android/view/View.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import android.os.Parcelable;
4747
import android.os.RemoteException;
4848
import android.os.SystemClock;
49+
import android.os.SystemProperties;
4950
import android.text.TextUtils;
5051
import android.util.AttributeSet;
5152
import android.util.FloatProperty;
@@ -16919,6 +16920,11 @@ public void setPooled(boolean isPooled) {
1691916920
*/
1692016921
Drawable mAccessibilityFocusDrawable;
1692116922

16923+
/**
16924+
* Show where the margins, bounds and layout bounds are for each view.
16925+
*/
16926+
final boolean mDebugLayout = SystemProperties.getBoolean("debug.layout", false);
16927+
1692216928
/**
1692316929
* Creates a new set of attachment information with the specified
1692416930
* events handler and thread.

core/java/android/view/ViewGroup.java

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import android.content.res.TypedArray;
2323
import android.graphics.Bitmap;
2424
import android.graphics.Canvas;
25+
import android.graphics.Color;
26+
import android.graphics.Insets;
2527
import android.graphics.Matrix;
2628
import android.graphics.Paint;
2729
import android.graphics.PointF;
@@ -420,9 +422,15 @@ public ViewGroup(Context context, AttributeSet attrs, int defStyle) {
420422
initFromAttributes(context, attrs);
421423
}
422424

425+
private boolean debugDraw() {
426+
return mAttachInfo != null && mAttachInfo.mDebugLayout;
427+
}
428+
423429
private void initViewGroup() {
424430
// ViewGroup doesn't draw by default
425-
setFlags(WILL_NOT_DRAW, DRAW_MASK);
431+
if (!debugDraw()) {
432+
setFlags(WILL_NOT_DRAW, DRAW_MASK);
433+
}
426434
mGroupFlags |= FLAG_CLIP_CHILDREN;
427435
mGroupFlags |= FLAG_CLIP_TO_PADDING;
428436
mGroupFlags |= FLAG_ANIMATION_DONE;
@@ -2650,6 +2658,52 @@ Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipCh
26502658
return b;
26512659
}
26522660

2661+
private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) {
2662+
canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint);
2663+
}
2664+
2665+
/**
2666+
* @hide
2667+
*/
2668+
protected void onDebugDrawMargins(Canvas canvas) {
2669+
for (int i = 0; i < getChildCount(); i++) {
2670+
View c = getChildAt(i);
2671+
c.getLayoutParams().onDebugDraw(this, canvas);
2672+
}
2673+
}
2674+
2675+
/**
2676+
* @hide
2677+
*/
2678+
protected void onDebugDraw(Canvas canvas) {
2679+
Paint paint = new Paint();
2680+
paint.setStyle(Paint.Style.STROKE);
2681+
2682+
// Draw optical bounds
2683+
if (getLayoutMode() == LAYOUT_BOUNDS) {
2684+
paint.setColor(Color.RED);
2685+
for (int i = 0; i < getChildCount(); i++) {
2686+
View c = getChildAt(i);
2687+
Insets insets = c.getLayoutInsets();
2688+
drawRect(canvas,
2689+
c.getLeft() + insets.left,
2690+
c.getTop() + insets.top,
2691+
c.getRight() - insets.right,
2692+
c.getBottom() - insets.bottom, paint);
2693+
}
2694+
}
2695+
2696+
// Draw bounds
2697+
paint.setColor(Color.BLUE);
2698+
for (int i = 0; i < getChildCount(); i++) {
2699+
View c = getChildAt(i);
2700+
drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint);
2701+
}
2702+
2703+
// Draw margins
2704+
onDebugDrawMargins(canvas);
2705+
}
2706+
26532707
/**
26542708
* {@inheritDoc}
26552709
*/
@@ -2741,6 +2795,10 @@ protected void dispatchDraw(Canvas canvas) {
27412795
}
27422796
}
27432797

2798+
if (debugDraw()) {
2799+
onDebugDraw(canvas);
2800+
}
2801+
27442802
if (clipToPadding) {
27452803
canvas.restoreToCount(saveCount);
27462804
}
@@ -5468,6 +5526,17 @@ public String debug(String output) {
54685526
+ sizeToString(width) + ", height=" + sizeToString(height) + " }";
54695527
}
54705528

5529+
/**
5530+
* Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
5531+
*
5532+
* @param view the view that contains these layout parameters
5533+
* @param canvas the canvas on which to draw
5534+
*
5535+
* @hide
5536+
*/
5537+
public void onDebugDraw(View view, Canvas canvas) {
5538+
}
5539+
54715540
/**
54725541
* Converts the specified size to a readable String.
54735542
*
@@ -5718,6 +5787,22 @@ public void onResolveLayoutDirection(int layoutDirection) {
57185787
break;
57195788
}
57205789
}
5790+
5791+
/**
5792+
* @hide
5793+
*/
5794+
@Override
5795+
public void onDebugDraw(View view, Canvas canvas) {
5796+
Paint paint = new Paint();
5797+
paint.setStyle(Paint.Style.STROKE);
5798+
paint.setColor(Color.MAGENTA);
5799+
5800+
drawRect(canvas,
5801+
view.getLeft() - leftMargin,
5802+
view.getTop() - topMargin,
5803+
view.getRight() + rightMargin,
5804+
view.getBottom() + bottomMargin, paint);
5805+
}
57215806
}
57225807

57235808
/* Describes a touched view and the ids of the pointers that it has captured.

core/java/android/widget/GridLayout.java

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import android.view.ViewGroup;
3131
import android.view.accessibility.AccessibilityEvent;
3232
import android.view.accessibility.AccessibilityNodeInfo;
33-
import com.android.internal.R;
3433
import android.widget.RemoteViews.RemoteView;
34+
import com.android.internal.R;
3535

3636
import java.lang.reflect.Array;
3737
import java.util.ArrayList;
@@ -209,7 +209,6 @@ public class GridLayout extends ViewGroup {
209209
// Misc constants
210210

211211
static final String TAG = GridLayout.class.getName();
212-
static final boolean DEBUG = false;
213212
static final int MAX_SIZE = 100000;
214213
static final int DEFAULT_CONTAINER_MARGIN = 0;
215214
static final int UNINITIALIZED_HASH = 0;
@@ -249,9 +248,6 @@ public class GridLayout extends ViewGroup {
249248
*/
250249
public GridLayout(Context context, AttributeSet attrs, int defStyle) {
251250
super(context, attrs, defStyle);
252-
if (DEBUG) {
253-
setWillNotDraw(false);
254-
}
255251
defaultGap = context.getResources().getDimensionPixelOffset(R.dimen.default_gap);
256252
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridLayout);
257253
try {
@@ -772,56 +768,53 @@ private void drawLine(Canvas graphics, int x1, int y1, int x2, int y2, Paint pai
772768
}
773769
}
774770

775-
private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) {
776-
canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint);
771+
/**
772+
* @hide
773+
*/
774+
@Override
775+
protected void onDebugDrawMargins(Canvas canvas) {
776+
// Apply defaults, so as to remove UNDEFINED values
777+
LayoutParams lp = new LayoutParams();
778+
for (int i = 0; i < getChildCount(); i++) {
779+
View c = getChildAt(i);
780+
lp.setMargins(
781+
getMargin1(c, true, true),
782+
getMargin1(c, false, true),
783+
getMargin1(c, true, false),
784+
getMargin1(c, false, false));
785+
lp.onDebugDraw(c, canvas);
786+
}
777787
}
778788

789+
/**
790+
* @hide
791+
*/
779792
@Override
780-
protected void onDraw(Canvas canvas) {
781-
super.onDraw(canvas);
782-
783-
if (DEBUG) {
784-
int height = getHeight() - getPaddingTop() - getPaddingBottom();
785-
int width = getWidth() - getPaddingLeft() - getPaddingRight();
786-
787-
Paint paint = new Paint();
788-
paint.setStyle(Paint.Style.STROKE);
789-
paint.setColor(Color.argb(50, 255, 255, 255));
790-
791-
int[] xs = horizontalAxis.locations;
792-
if (xs != null) {
793-
for (int i = 0, length = xs.length; i < length; i++) {
794-
int x = xs[i];
795-
drawLine(canvas, x, 0, x, height - 1, paint);
796-
}
797-
}
793+
protected void onDebugDraw(Canvas canvas) {
794+
int height = getHeight() - getPaddingTop() - getPaddingBottom();
795+
int width = getWidth() - getPaddingLeft() - getPaddingRight();
798796

799-
int[] ys = verticalAxis.locations;
800-
if (ys != null) {
801-
for (int i = 0, length = ys.length; i < length; i++) {
802-
int y = ys[i];
803-
drawLine(canvas, 0, y, width - 1, y, paint);
804-
}
805-
}
797+
Paint paint = new Paint();
798+
paint.setStyle(Paint.Style.STROKE);
799+
paint.setColor(Color.argb(50, 255, 255, 255));
806800

807-
// Draw bounds
808-
paint.setColor(Color.BLUE);
809-
for (int i = 0; i < getChildCount(); i++) {
810-
View c = getChildAt(i);
811-
drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint);
801+
int[] xs = horizontalAxis.locations;
802+
if (xs != null) {
803+
for (int i = 0, length = xs.length; i < length; i++) {
804+
int x = xs[i];
805+
drawLine(canvas, x, 0, x, height - 1, paint);
812806
}
807+
}
813808

814-
// Draw margins
815-
paint.setColor(Color.MAGENTA);
816-
for (int i = 0; i < getChildCount(); i++) {
817-
View c = getChildAt(i);
818-
drawRect(canvas,
819-
c.getLeft() - getMargin1(c, true, true),
820-
c.getTop() - getMargin1(c, false, true),
821-
c.getRight() + getMargin1(c, true, false),
822-
c.getBottom() + getMargin1(c, false, false), paint);
809+
int[] ys = verticalAxis.locations;
810+
if (ys != null) {
811+
for (int i = 0, length = ys.length; i < length; i++) {
812+
int y = ys[i];
813+
drawLine(canvas, 0, y, width - 1, y, paint);
823814
}
824815
}
816+
817+
super.onDebugDraw(canvas);
825818
}
826819

827820
// Add/remove

tests/GridLayoutTest/src/com/android/test/layout/LayoutInsetsTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5-
import android.graphics.Rect;
6-
import android.graphics.drawable.Drawable;
75
import android.os.Build;
86
import android.os.Bundle;
97
import android.view.View;
108
import android.widget.Button;
11-
import android.widget.EditText;
129
import android.widget.GridLayout;
13-
import android.widget.Space;
1410
import android.widget.TextView;
1511

16-
import static android.text.InputType.TYPE_CLASS_TEXT;
17-
import static android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
18-
import static android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;
1912
import static android.widget.GridLayout.*;
20-
import static android.widget.GridLayout.FILL;
21-
import static android.widget.GridLayout.spec;
2213

2314
public class LayoutInsetsTest extends Activity {
2415
public static View create(Context context) {

0 commit comments

Comments
 (0)