diff options
author | Philip Milne <pmilne@google.com> | 2012-04-23 15:38:27 -0700 |
---|---|---|
committer | Philip Milne <pmilne@google.com> | 2012-04-23 16:41:04 -0700 |
commit | 10ca24a97cefc14fca1b26f59e627f487b3b108b (patch) | |
tree | fcdcad2bf4fb8df3e780abd31a9a1b62d5bb1c1f /core/java/android/view/ViewGroup.java | |
parent | 6ec0c6afafd9bad5e4c33578e9355997a280649c (diff) | |
download | frameworks_base-10ca24a97cefc14fca1b26f59e627f487b3b108b.zip frameworks_base-10ca24a97cefc14fca1b26f59e627f487b3b108b.tar.gz frameworks_base-10ca24a97cefc14fca1b26f59e627f487b3b108b.tar.bz2 |
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
Diffstat (limited to 'core/java/android/view/ViewGroup.java')
-rw-r--r-- | core/java/android/view/ViewGroup.java | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java index 91e945b..d05d19b 100644 --- a/core/java/android/view/ViewGroup.java +++ b/core/java/android/view/ViewGroup.java @@ -22,6 +22,8 @@ import android.content.res.Configuration; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Insets; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.PointF; @@ -420,9 +422,15 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager initFromAttributes(context, attrs); } + private boolean debugDraw() { + return mAttachInfo != null && mAttachInfo.mDebugLayout; + } + private void initViewGroup() { // ViewGroup doesn't draw by default - setFlags(WILL_NOT_DRAW, DRAW_MASK); + if (!debugDraw()) { + setFlags(WILL_NOT_DRAW, DRAW_MASK); + } mGroupFlags |= FLAG_CLIP_CHILDREN; mGroupFlags |= FLAG_CLIP_TO_PADDING; mGroupFlags |= FLAG_ANIMATION_DONE; @@ -2584,6 +2592,52 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager return b; } + private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) { + canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint); + } + + /** + * @hide + */ + protected void onDebugDrawMargins(Canvas canvas) { + for (int i = 0; i < getChildCount(); i++) { + View c = getChildAt(i); + c.getLayoutParams().onDebugDraw(this, canvas); + } + } + + /** + * @hide + */ + protected void onDebugDraw(Canvas canvas) { + Paint paint = new Paint(); + paint.setStyle(Paint.Style.STROKE); + + // Draw optical bounds + if (getLayoutMode() == LAYOUT_BOUNDS) { + paint.setColor(Color.RED); + for (int i = 0; i < getChildCount(); i++) { + View c = getChildAt(i); + Insets insets = c.getLayoutInsets(); + drawRect(canvas, + c.getLeft() + insets.left, + c.getTop() + insets.top, + c.getRight() - insets.right, + c.getBottom() - insets.bottom, paint); + } + } + + // Draw bounds + paint.setColor(Color.BLUE); + for (int i = 0; i < getChildCount(); i++) { + View c = getChildAt(i); + drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint); + } + + // Draw margins + onDebugDrawMargins(canvas); + } + /** * {@inheritDoc} */ @@ -2675,6 +2729,10 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager } } + if (debugDraw()) { + onDebugDraw(canvas); + } + if (clipToPadding) { canvas.restoreToCount(saveCount); } @@ -5393,6 +5451,17 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager } /** + * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. + * + * @param view the view that contains these layout parameters + * @param canvas the canvas on which to draw + * + * @hide + */ + public void onDebugDraw(View view, Canvas canvas) { + } + + /** * Converts the specified size to a readable String. * * @param size the size to convert @@ -5642,6 +5711,22 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager break; } } + + /** + * @hide + */ + @Override + public void onDebugDraw(View view, Canvas canvas) { + Paint paint = new Paint(); + paint.setStyle(Paint.Style.STROKE); + paint.setColor(Color.MAGENTA); + + drawRect(canvas, + view.getLeft() - leftMargin, + view.getTop() - topMargin, + view.getRight() + rightMargin, + view.getBottom() + bottomMargin, paint); + } } /* Describes a touched view and the ids of the pointers that it has captured. |