Bug 833800: FadedTextView should handle compound drawables. [r=lucasr, a=lsblakk]
authorSriram Ramasubramanian <sriram@mozilla.com>
Tue, 17 Sep 2013 13:59:10 -0400
changeset 223612 cdd031129be5daca8aabdea8e8d3be426fc2253e
parent 223611 067c524d378fb355f7b633c8c55c464d4e6e1b9c
child 223613 ad6e4eda7e5c3a133a94d25b9e1dd710948c1912
push id6
push userryanvm@gmail.com
push dateMon, 12 Jan 2015 22:04:06 +0000
treeherdermozilla-b2g37_v2_2@895c8fc7b734 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerslucasr, lsblakk
bugs833800
milestone26.0a2
Bug 833800: FadedTextView should handle compound drawables. [r=lucasr, a=lsblakk]
mobile/android/base/home/FadedTextView.java
--- a/mobile/android/base/home/FadedTextView.java
+++ b/mobile/android/base/home/FadedTextView.java
@@ -5,61 +5,74 @@
 
 package org.mozilla.gecko.home;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.LinearGradient;
 import android.graphics.Shader;
+import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.widget.TextView;
 
 import org.mozilla.gecko.R;
 
 /**
  * FadedTextView fades the ends of the text by fadeWidth amount,
  * if the text is too long and requires an ellipsis.
  */
 public class FadedTextView extends TextView {
 
     // Width of the fade effect from end of the view.
     private int mFadeWidth;
 
+    // Padding for compound drawables.
+    private int mCompoundPadding;
+
     public FadedTextView(Context context) {
         this(context, null);
     }
 
     public FadedTextView(Context context, AttributeSet attrs) {
         this(context, attrs, android.R.attr.textViewStyle);
     }
 
     public FadedTextView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
 
         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView);
         mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0);
         a.recycle();
+
+        mCompoundPadding = getCompoundDrawablePadding();
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
     public void onDraw(Canvas canvas) {
         int width = getMeasuredWidth();
 
         // Layout doesn't return a proper width for getWidth().
         // Instead check the width of the first line, as we've restricted to just one line.
         if (getLayout().getLineWidth(0) > width) {
+            final Drawable leftDrawable = getCompoundDrawables()[0];
+            int drawableWidth = 0;
+            if (leftDrawable != null) {
+                drawableWidth = leftDrawable.getIntrinsicWidth() + mCompoundPadding;
+                width -= drawableWidth;
+            }
+
             int color = getCurrentTextColor();
             float stop = ((float) (width - mFadeWidth) / (float) width);
             LinearGradient gradient = new LinearGradient(0, 0, width, 0,
                                                          new int[] { color, color, 0x0 },
-                                                         new float[] { 0, stop, 1.0f },
+                                                         new float[] { 0, stop, 1.0f - (drawableWidth / width) },
                                                          Shader.TileMode.CLAMP);
             getPaint().setShader(gradient);
         } else {
             getPaint().setShader(null);
         }
 
         // Do a default draw.
         super.onDraw(canvas);