summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/widget/FadedMultiColorTextView.java
blob: 08bb55ef6302bc9acc1732dcc5a9482298033536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.widget;

import org.mozilla.gecko.R;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;

/**
 * Fades the end of the text by gecko:fadeWidth amount,
 * if the text is too long and requires an ellipsis.
 *
 * This implementation is an improvement over Android's built-in fadingEdge
 * but potentially slower than the {@link org.mozilla.gecko.widget.FadedSingleColorTextView}.
 * It works for text of multiple colors but only one background color. It works by
 * drawing a gradient rectangle with the background color over the text, fading it out.
 */
public class FadedMultiColorTextView extends FadedTextView {
    private final ColorStateList fadeBackgroundColorList;

    private final Paint fadePaint;
    private FadedTextGradient backgroundGradient;

    public FadedMultiColorTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        fadePaint = new Paint();

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedMultiColorTextView);
        fadeBackgroundColorList =
                a.getColorStateList(R.styleable.FadedMultiColorTextView_fadeBackgroundColor);
        a.recycle();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final boolean needsEllipsis = needsEllipsis();
        if (needsEllipsis) {
            final int right = getWidth() - getCompoundPaddingRight();
            final float left = right - fadeWidth;

            updateGradientShader(needsEllipsis, right);

            // Shrink height of gradient to prevent it overlaying parent view border.
            // The shrunk size just nee to cover the text itself.
            final float density = getResources().getDisplayMetrics().density;
            final float h = Math.abs(fadePaint.getFontMetrics().top) + 1;
            final float l = fadePaint.getFontMetrics().bottom + 1;
            final float top = getBaseline() - h * density;
            final float bottom = getBaseline() + l * density;

            canvas.drawRect(left, top, right, bottom, fadePaint);
        }
    }

    private void updateGradientShader(final boolean needsEllipsis, final int gradientEndRight) {
        final int backgroundColor =
                fadeBackgroundColorList.getColorForState(getDrawableState(), Color.RED);

        final boolean needsNewGradient = (backgroundGradient == null ||
                                          backgroundGradient.getBackgroundColor() != backgroundColor ||
                                          backgroundGradient.getEndRight() != gradientEndRight);

        if (needsEllipsis && needsNewGradient) {
            backgroundGradient = new FadedTextGradient(gradientEndRight, fadeWidth, backgroundColor);
            fadePaint.setShader(backgroundGradient);
        }
    }

    private static class FadedTextGradient extends LinearGradient {
        private final int endRight;
        private final int backgroundColor;

        public FadedTextGradient(final int gradientEndRight, final int fadeWidth,
                final int backgroundColor) {
            super(gradientEndRight - fadeWidth, 0, gradientEndRight, 0,
                  getColorWithZeroedAlpha(backgroundColor), backgroundColor, Shader.TileMode.CLAMP);

            this.endRight = gradientEndRight;
            this.backgroundColor = backgroundColor;
        }

        private static int getColorWithZeroedAlpha(final int color) {
            return Color.argb(0, Color.red(color), Color.green(color), Color.blue(color));
        }

        public int getEndRight() {
            return endRight;
        }

        public int getBackgroundColor() {
            return backgroundColor;
        }
    }
}