Skip to content

Commit 10ca24a

Browse files
author
Philip Milne
committed
Promote layout debugging code from GridLayout to ViewGroup.
Layout debugging code draws rectangles around: 1. Layout insets (red) 2. Bounds (blue) 3. Margins (magenta) Layout debug mode is enabled with: adb shell setprop debug.layout true Change-Id: Ia155a2d0fbf33693a1e3c040f627ea3a534e1aff
1 parent 6ec0c6a commit 10ca24a

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
@@ -45,6 +45,7 @@
4545
import android.os.Parcelable;
4646
import android.os.RemoteException;
4747
import android.os.SystemClock;
48+
import android.os.SystemProperties;
4849
import android.text.TextUtils;
4950
import android.util.AttributeSet;
5051
import android.util.FloatProperty;
@@ -16913,6 +16914,11 @@ public void setPooled(boolean isPooled) {
1691316914
*/
1691416915
Drawable mAccessibilityFocusDrawable;
1691516916

16917+
/**
16918+
* Show where the margins, bounds and layout bounds are for each view.
16919+
*/
16920+
final boolean mDebugLayout = SystemProperties.getBoolean("debug.layout", false);
16921+
1691616922
/**
1691716923
* Creates a new set of attachment information with the specified
1691816924
* 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;
@@ -2584,6 +2592,52 @@ Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipCh
25842592
return b;
25852593
}
25862594

2595+
private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) {
2596+
canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint);
2597+
}
2598+
2599+
/**
2600+
* @hide
2601+
*/
2602+
protected void onDebugDrawMargins(Canvas canvas) {
2603+
for (int i = 0; i < getChildCount(); i++) {
2604+
View c = getChildAt(i);
2605+
c.getLayoutParams().onDebugDraw(this, canvas);
2606+
}
2607+
}
2608+
2609+
/**
2610+
* @hide
2611+
*/
2612+
protected void onDebugDraw(Canvas canvas) {
2613+
Paint paint = new Paint();
2614+
paint.setStyle(Paint.Style.STROKE);
2615+
2616+
// Draw optical bounds
2617+
if (getLayoutMode() == LAYOUT_BOUNDS) {
2618+
paint.setColor(Color.RED);
2619+
for (int i = 0; i < getChildCount(); i++) {
2620+
View c = getChildAt(i);
2621+
Insets insets = c.getLayoutInsets();
2622+
drawRect(canvas,
2623+
c.getLeft() + insets.left,
2624+
c.getTop() + insets.top,
2625+
c.getRight() - insets.right,
2626+
c.getBottom() - insets.bottom, paint);
2627+
}
2628+
}
2629+
2630+
// Draw bounds
2631+
paint.setColor(Color.BLUE);
2632+
for (int i = 0; i < getChildCount(); i++) {
2633+
View c = getChildAt(i);
2634+
drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint);
2635+
}
2636+
2637+
// Draw margins
2638+
onDebugDrawMargins(canvas);
2639+
}
2640+
25872641
/**
25882642
* {@inheritDoc}
25892643
*/
@@ -2675,6 +2729,10 @@ protected void dispatchDraw(Canvas canvas) {
26752729
}
26762730
}
26772731

2732+
if (debugDraw()) {
2733+
onDebugDraw(canvas);
2734+
}
2735+
26782736
if (clipToPadding) {
26792737
canvas.restoreToCount(saveCount);
26802738
}
@@ -5392,6 +5450,17 @@ public String debug(String output) {
53925450
+ sizeToString(width) + ", height=" + sizeToString(height) + " }";
53935451
}
53945452

5453+
/**
5454+
* Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
5455+
*
5456+
* @param view the view that contains these layout parameters
5457+
* @param canvas the canvas on which to draw
5458+
*
5459+
* @hide
5460+
*/
5461+
public void onDebugDraw(View view, Canvas canvas) {
5462+
}
5463+
53955464
/**
53965465
* Converts the specified size to a readable String.
53975466
*
@@ -5642,6 +5711,22 @@ public void onResolveLayoutDirection(int layoutDirection) {
56425711
break;
56435712
}
56445713
}
5714+
5715+
/**
5716+
* @hide
5717+
*/
5718+
@Override
5719+
public void onDebugDraw(View view, Canvas canvas) {
5720+
Paint paint = new Paint();
5721+
paint.setStyle(Paint.Style.STROKE);
5722+
paint.setColor(Color.MAGENTA);
5723+
5724+
drawRect(canvas,
5725+
view.getLeft() - leftMargin,
5726+
view.getTop() - topMargin,
5727+
view.getRight() + rightMargin,
5728+
view.getBottom() + bottomMargin, paint);
5729+
}
56455730
}
56465731

56475732
/* 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)