mobile/android/base/home/FadedTextView.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/home/FadedTextView.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.home;
    1.10 +
    1.11 +import android.content.Context;
    1.12 +import android.content.res.TypedArray;
    1.13 +import android.graphics.Canvas;
    1.14 +import android.graphics.LinearGradient;
    1.15 +import android.graphics.Shader;
    1.16 +import android.graphics.drawable.Drawable;
    1.17 +import android.util.AttributeSet;
    1.18 +import android.widget.TextView;
    1.19 +
    1.20 +import org.mozilla.gecko.R;
    1.21 +
    1.22 +/**
    1.23 + * FadedTextView fades the ends of the text by fadeWidth amount,
    1.24 + * if the text is too long and requires an ellipsis.
    1.25 + */
    1.26 +public class FadedTextView extends TextView {
    1.27 +
    1.28 +    // Width of the fade effect from end of the view.
    1.29 +    private int mFadeWidth;
    1.30 +
    1.31 +    // Padding for compound drawables.
    1.32 +    private int mCompoundPadding;
    1.33 +
    1.34 +    public FadedTextView(Context context) {
    1.35 +        this(context, null);
    1.36 +    }
    1.37 +
    1.38 +    public FadedTextView(Context context, AttributeSet attrs) {
    1.39 +        this(context, attrs, android.R.attr.textViewStyle);
    1.40 +    }
    1.41 +
    1.42 +    public FadedTextView(Context context, AttributeSet attrs, int defStyle) {
    1.43 +        super(context, attrs, defStyle);
    1.44 +
    1.45 +        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView);
    1.46 +        mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0);
    1.47 +        a.recycle();
    1.48 +
    1.49 +        mCompoundPadding = getCompoundDrawablePadding();
    1.50 +    }
    1.51 +
    1.52 +    /**
    1.53 +     * {@inheritDoc}
    1.54 +     */
    1.55 +    @Override
    1.56 +    public void onDraw(Canvas canvas) {
    1.57 +        int width = getMeasuredWidth();
    1.58 +
    1.59 +        // Layout doesn't return a proper width for getWidth().
    1.60 +        // Instead check the width of the first line, as we've restricted to just one line.
    1.61 +        if (getLayout().getLineWidth(0) > width) {
    1.62 +            final Drawable leftDrawable = getCompoundDrawables()[0];
    1.63 +            int drawableWidth = 0;
    1.64 +            if (leftDrawable != null) {
    1.65 +                drawableWidth = leftDrawable.getIntrinsicWidth() + mCompoundPadding;
    1.66 +                width -= drawableWidth;
    1.67 +            }
    1.68 +
    1.69 +            int color = getCurrentTextColor();
    1.70 +            float stop = ((float) (width - mFadeWidth) / (float) width);
    1.71 +            LinearGradient gradient = new LinearGradient(0, 0, width, 0,
    1.72 +                                                         new int[] { color, color, 0x0 },
    1.73 +                                                         new float[] { 0, stop, 1.0f - (drawableWidth / width) },
    1.74 +                                                         Shader.TileMode.CLAMP);
    1.75 +            getPaint().setShader(gradient);
    1.76 +        } else {
    1.77 +            getPaint().setShader(null);
    1.78 +        }
    1.79 +
    1.80 +        // Do a default draw.
    1.81 +        super.onDraw(canvas);
    1.82 +    }
    1.83 +}

mercurial