From 856e2a9d72fe3a79cd7bfa5cf443d13a60495c4b Mon Sep 17 00:00:00 2001 From: Mark Wei Date: Tue, 14 Apr 2015 10:53:05 -0700 Subject: Update bitmap drawables with RTL support. Change-Id: Iafcbb41208b2e7e3396f2b86bee994cbaf57009f --- .../bitmap/drawable/BasicBitmapDrawable.java | 36 ++++++++++++++ .../drawable/StyledCornersBitmapDrawable.java | 58 ++++++++++++++++------ .../bitmap/view/BitmapDrawableImageView.java | 8 +++ 3 files changed, 88 insertions(+), 14 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java index 67884df..ca429ee 100644 --- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java @@ -73,6 +73,8 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, private DecodeTask mTask; private Cancelable mCreateFileDescriptorFactoryTask; + private int mLayoutDirection; + // based on framework CL:I015d77 private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); private static final int CORE_POOL_SIZE = CPU_COUNT + 1; @@ -127,6 +129,40 @@ public class BasicBitmapDrawable extends Drawable implements DecodeCallback, } } + /** + * Set layout direction. + * It ends with Local so as not conflict with hidden Drawable.setLayoutDirection. + * @param layoutDirection the resolved layout direction for the drawable, + * either {@link android.view.View#LAYOUT_DIRECTION_LTR} + * or {@link android.view.View#LAYOUT_DIRECTION_RTL} + */ + public void setLayoutDirectionLocal(int layoutDirection) { + if (mLayoutDirection != layoutDirection) { + mLayoutDirection = layoutDirection; + onLayoutDirectionChangeLocal(layoutDirection); + } + } + + /** + * Called when the drawable's resolved layout direction changes. + * It ends with Local so as not conflict with hidden Drawable.onLayoutDirectionChange. + * + * @param layoutDirection the new resolved layout direction + */ + public void onLayoutDirectionChangeLocal(int layoutDirection) {} + + /** + * Returns the resolved layout direction for this Drawable. + * It ends with Local so as not conflict with hidden Drawable.getLayoutDirection. + * + * @return One of {@link android.view.View#LAYOUT_DIRECTION_LTR}, + * {@link android.view.View#LAYOUT_DIRECTION_RTL} + * @see #setLayoutDirectionLocal(int) + */ + public int getLayoutDirectionLocal() { + return mLayoutDirection; + } + /** * Binds to the given key and start the decode process. This will first look in the cache, then * decode from the request key if not found. diff --git a/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java b/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java index 953bfd0..5582344 100644 --- a/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java +++ b/src/com/android/bitmap/drawable/StyledCornersBitmapDrawable.java @@ -25,6 +25,7 @@ import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.util.Log; +import android.view.View; import com.android.bitmap.BitmapCache; @@ -65,6 +66,12 @@ public class StyledCornersBitmapDrawable extends ExtendedBitmapDrawable { private int mTopRightCornerStyle = CORNER_STYLE_SHARP; private int mBottomRightCornerStyle = CORNER_STYLE_SHARP; private int mBottomLeftCornerStyle = CORNER_STYLE_SHARP; + + private int mTopStartCornerStyle = CORNER_STYLE_SHARP; + private int mTopEndCornerStyle = CORNER_STYLE_SHARP; + private int mBottomEndCornerStyle = CORNER_STYLE_SHARP; + private int mBottomStartCornerStyle = CORNER_STYLE_SHARP; + private int mScrimColor; private float mBorderWidth; private boolean mIsCompatibilityMode; @@ -122,21 +129,18 @@ public class StyledCornersBitmapDrawable extends ExtendedBitmapDrawable { } } - /** Set the corner styles for all four corners */ - public void setCornerStyles(int topLeft, int topRight, int bottomRight, int bottomLeft) { - boolean changed = mTopLeftCornerStyle != topLeft - || mTopRightCornerStyle != topRight - || mBottomRightCornerStyle != bottomRight - || mBottomLeftCornerStyle != bottomLeft; - - mTopLeftCornerStyle = topLeft; - mTopRightCornerStyle = topRight; - mBottomRightCornerStyle = bottomRight; - mBottomLeftCornerStyle = bottomLeft; + /** Set the corner styles for all four corners specified in RTL friendly ways */ + public void setCornerStylesRelative(int topStart, int topEnd, int bottomEnd, int bottomStart) { + mTopStartCornerStyle = topStart; + mTopEndCornerStyle = topEnd; + mBottomEndCornerStyle = bottomEnd; + mBottomStartCornerStyle = bottomStart; + resolveCornerStyles(); + } - if (changed) { - recalculatePath(); - } + @Override + public void onLayoutDirectionChangeLocal(int layoutDirection) { + resolveCornerStyles(); } /** @@ -462,4 +466,30 @@ public class StyledCornersBitmapDrawable extends ExtendedBitmapDrawable { // Finish. mClipPath.close(); } + + private void resolveCornerStyles() { + boolean isLtr = getLayoutDirectionLocal() == View.LAYOUT_DIRECTION_LTR; + setCornerStyles( + isLtr ? mTopStartCornerStyle : mTopEndCornerStyle, + isLtr ? mTopEndCornerStyle : mTopStartCornerStyle, + isLtr ? mBottomEndCornerStyle : mBottomStartCornerStyle, + isLtr ? mBottomStartCornerStyle : mBottomEndCornerStyle); + } + + /** Set the corner styles for all four corners */ + private void setCornerStyles(int topLeft, int topRight, int bottomRight, int bottomLeft) { + boolean changed = mTopLeftCornerStyle != topLeft + || mTopRightCornerStyle != topRight + || mBottomRightCornerStyle != bottomRight + || mBottomLeftCornerStyle != bottomLeft; + + mTopLeftCornerStyle = topLeft; + mTopRightCornerStyle = topRight; + mBottomRightCornerStyle = bottomRight; + mBottomLeftCornerStyle = bottomLeft; + + if (changed) { + recalculatePath(); + } + } } diff --git a/src/com/android/bitmap/view/BitmapDrawableImageView.java b/src/com/android/bitmap/view/BitmapDrawableImageView.java index 5cbc58d..12e5b0e 100644 --- a/src/com/android/bitmap/view/BitmapDrawableImageView.java +++ b/src/com/android/bitmap/view/BitmapDrawableImageView.java @@ -157,4 +157,12 @@ public class BitmapDrawableImageView extends ImageView { unbindDrawable(TEMPORARY); } } + + @Override + public void onRtlPropertiesChanged(int layoutDirection) { + super.onRtlPropertiesChanged(layoutDirection); + if (mDrawable != null) { + mDrawable.setLayoutDirectionLocal(layoutDirection); + } + } } -- cgit v1.2.3