Bug 1105472 - Part 1: Make FadedTextView abstract and move current implementation to FadedSingleColorTextView. r=bnicholson

This commit is contained in:
Michael Comella
2014-12-31 12:31:17 -08:00
parent 2fd18c991c
commit 166f1517d9
10 changed files with 109 additions and 90 deletions

View File

@@ -7,48 +7,35 @@ package org.mozilla.gecko.widget;
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.text.Layout;
import android.util.AttributeSet;
import org.mozilla.gecko.R;
import org.mozilla.gecko.widget.ThemedTextView;
/**
* FadedTextView fades the ends of the text by fadeWidth amount,
* if the text is too long and requires an ellipsis.
* An implementation of FadedTextView should fade the end of the text
* by gecko:fadeWidth amount, if the text is too long and requires an ellipsis.
*/
public class FadedTextView extends ThemedTextView {
public abstract class FadedTextView extends ThemedTextView {
// Width of the fade effect from end of the view.
private final int mFadeWidth;
protected final int fadeWidth;
// Shader for the fading edge.
private FadedTextGradient mTextGradient;
public FadedTextView(Context context) {
this(context, null);
}
public FadedTextView(Context context, AttributeSet attrs) {
public FadedTextView(final Context context, final AttributeSet attrs) {
super(context, attrs);
setSingleLine(true);
setEllipsize(null);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView);
mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView);
fadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0);
a.recycle();
}
private int getAvailableWidth() {
protected int getAvailableWidth() {
return getWidth() - getCompoundPaddingLeft() - getCompoundPaddingRight();
}
private boolean needsEllipsis() {
protected boolean needsEllipsis() {
final int width = getAvailableWidth();
if (width <= 0) {
return false;
@@ -57,49 +44,4 @@ public class FadedTextView extends ThemedTextView {
final Layout layout = getLayout();
return (layout != null && layout.getLineWidth(0) > width);
}
private void updateGradientShader() {
final int color = getCurrentTextColor();
final int width = getAvailableWidth();
final boolean needsNewGradient = (mTextGradient == null ||
mTextGradient.getColor() != color ||
mTextGradient.getWidth() != width);
final boolean needsEllipsis = needsEllipsis();
if (needsEllipsis && needsNewGradient) {
mTextGradient = new FadedTextGradient(width, mFadeWidth, color);
}
getPaint().setShader(needsEllipsis ? mTextGradient : null);
}
@Override
public void onDraw(Canvas canvas) {
updateGradientShader();
super.onDraw(canvas);
}
private static class FadedTextGradient extends LinearGradient {
private final int mWidth;
private final int mColor;
public FadedTextGradient(int width, int fadeWidth, int color) {
super(0, 0, width, 0,
new int[] { color, color, 0x0 },
new float[] { 0, ((float) (width - fadeWidth) / width), 1.0f },
Shader.TileMode.CLAMP);
mWidth = width;
mColor = color;
}
public int getWidth() {
return mWidth;
}
public int getColor() {
return mColor;
}
}
}