From 3c77d39ee8b1c3c5a1b59be65953f73570a42511 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Wed, 20 May 2009 11:26:50 -0700 Subject: [PATCH] Fixes #1866819.\nTextView sets the bounds of its compound drawables to contain only the width and the height of the drawables. This causes View.invalidateDrawable() to invalidate the compound drawables at location (0, 0) within the view, thus invalidating the wrong part of the View. This prevents animation from running correctly when using animated drawable. This change overrides invalidateDrawable() in TextView to take into account the real position of the compound drawable. --- core/java/android/widget/TextView.java | 54 ++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 4297be0b52432..b7fbc0ec8186f 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -3700,6 +3700,52 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener return verified; } + @Override + public void invalidateDrawable(Drawable drawable) { + if (verifyDrawable(drawable)) { + final Rect dirty = drawable.getBounds(); + int scrollX = mScrollX; + int scrollY = mScrollY; + + // IMPORTANT: The coordinates below are based on the coordinates computed + // for each compound drawable in onDraw(). Make sure to update each section + // accordingly. + final TextView.Drawables drawables = mDrawables; + if (drawable == drawables.mDrawableLeft) { + final int compoundPaddingTop = getCompoundPaddingTop(); + final int compoundPaddingBottom = getCompoundPaddingBottom(); + final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; + + scrollX += mPaddingLeft; + scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightLeft) / 2; + } else if (drawable == drawables.mDrawableRight) { + final int compoundPaddingTop = getCompoundPaddingTop(); + final int compoundPaddingBottom = getCompoundPaddingBottom(); + final int vspace = mBottom - mTop - compoundPaddingBottom - compoundPaddingTop; + + scrollX += (mRight - mLeft - mPaddingRight - drawables.mDrawableSizeRight); + scrollY += compoundPaddingTop + (vspace - drawables.mDrawableHeightRight) / 2; + } else if (drawable == drawables.mDrawableTop) { + final int compoundPaddingLeft = getCompoundPaddingLeft(); + final int compoundPaddingRight = getCompoundPaddingRight(); + final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; + + scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthTop) / 2; + scrollY += mPaddingTop; + } else if (drawable == drawables.mDrawableBottom) { + final int compoundPaddingLeft = getCompoundPaddingLeft(); + final int compoundPaddingRight = getCompoundPaddingRight(); + final int hspace = mRight - mLeft - compoundPaddingRight - compoundPaddingLeft; + + scrollX += compoundPaddingLeft + (hspace - drawables.mDrawableWidthBottom) / 2; + scrollY += (mBottom - mTop - mPaddingBottom - drawables.mDrawableSizeBottom); + } + + invalidate(dirty.left + scrollX, dirty.top + scrollY, + dirty.right + scrollX, dirty.bottom + scrollY); + } + } + @Override protected void onDraw(Canvas canvas) { restartMarqueeIfNeeded(); @@ -3728,6 +3774,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop; int hspace = right - left - compoundPaddingRight - compoundPaddingLeft; + // IMPORTANT: The coordinates computed are also used in invalidateDrawable() + // Make sure to update invalidateDrawable() when changing this code. if (dr.mDrawableLeft != null) { canvas.save(); canvas.translate(scrollX + mPaddingLeft, @@ -3737,6 +3785,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener canvas.restore(); } + // IMPORTANT: The coordinates computed are also used in invalidateDrawable() + // Make sure to update invalidateDrawable() when changing this code. if (dr.mDrawableRight != null) { canvas.save(); canvas.translate(scrollX + right - left - mPaddingRight - dr.mDrawableSizeRight, @@ -3745,6 +3795,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener canvas.restore(); } + // IMPORTANT: The coordinates computed are also used in invalidateDrawable() + // Make sure to update invalidateDrawable() when changing this code. if (dr.mDrawableTop != null) { canvas.save(); canvas.translate(scrollX + compoundPaddingLeft + (hspace - dr.mDrawableWidthTop) / 2, @@ -3753,6 +3805,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener canvas.restore(); } + // IMPORTANT: The coordinates computed are also used in invalidateDrawable() + // Make sure to update invalidateDrawable() when changing this code. if (dr.mDrawableBottom != null) { canvas.save(); canvas.translate(scrollX + compoundPaddingLeft +