From 3b983a74c6bac40aad191dfcfbed930cd25a9a01 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Thu, 5 Jun 2014 13:43:25 -0700 Subject: [PATCH] Fix default stroke width and NPE for line with no stroke width Also fixes restart on visiblity change in AnimationDrawable BUG: 15437284 Change-Id: I698e7ffb11489061a38fbcd5cea10ba11a6abb34 --- .../graphics/drawable/AnimationDrawable.java | 2 +- .../graphics/drawable/GradientDrawable.java | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/graphics/java/android/graphics/drawable/AnimationDrawable.java b/graphics/java/android/graphics/drawable/AnimationDrawable.java index 0ee253a2e460e..16548d0801ef2 100644 --- a/graphics/java/android/graphics/drawable/AnimationDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimationDrawable.java @@ -94,7 +94,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An boolean changed = super.setVisible(visible, restart); if (visible) { if (changed || restart) { - setFrame(0, true, mCurFrame >= 0); + setFrame(0, true, restart || mCurFrame >= 0); } } else { unscheduleSelf(this); diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index 1512da5b3ff87..241b89e2d6b89 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -1203,8 +1203,11 @@ public class GradientDrawable extends Drawable { st.mAttrStroke = a.extractThemeAttrs(); + // We have an explicit stroke defined, so the default stroke width + // must be at least 0 or the current stroke width. + final int defaultStrokeWidth = Math.max(0, st.mStrokeWidth); final int width = a.getDimensionPixelSize( - R.styleable.GradientDrawableStroke_width, st.mStrokeWidth); + R.styleable.GradientDrawableStroke_width, defaultStrokeWidth); final float dashWidth = a.getDimension( R.styleable.GradientDrawableStroke_dashWidth, st.mStrokeDashWidth); @@ -1406,10 +1409,13 @@ public class GradientDrawable extends Drawable { outline.setOval(bounds); return true; case LINE: - float halfStrokeWidth = mStrokePaint.getStrokeWidth() * 0.5f; - float centerY = bounds.centerY(); - int top = (int) Math.floor(centerY - halfStrokeWidth); - int bottom = (int) Math.ceil(centerY + halfStrokeWidth); + // Hairlines (0-width stroke) must have a non-empty outline for + // shadows to draw correctly, so we'll use a very small width. + final float halfStrokeWidth = mStrokePaint == null ? + 0.0001f : mStrokePaint.getStrokeWidth() * 0.5f; + final float centerY = bounds.centerY(); + final int top = (int) Math.floor(centerY - halfStrokeWidth); + final int bottom = (int) Math.ceil(centerY + halfStrokeWidth); outline.setRect(bounds.left, top, bounds.right, bottom); return true;