michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.TypedArray; michael@0: import android.graphics.Canvas; michael@0: import android.graphics.LinearGradient; michael@0: import android.graphics.Shader; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.widget.TextView; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: /** michael@0: * FadedTextView fades the ends of the text by fadeWidth amount, michael@0: * if the text is too long and requires an ellipsis. michael@0: */ michael@0: public class FadedTextView extends TextView { michael@0: michael@0: // Width of the fade effect from end of the view. michael@0: private int mFadeWidth; michael@0: michael@0: // Padding for compound drawables. michael@0: private int mCompoundPadding; michael@0: michael@0: public FadedTextView(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public FadedTextView(Context context, AttributeSet attrs) { michael@0: this(context, attrs, android.R.attr.textViewStyle); michael@0: } michael@0: michael@0: public FadedTextView(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: michael@0: TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FadedTextView); michael@0: mFadeWidth = a.getDimensionPixelSize(R.styleable.FadedTextView_fadeWidth, 0); michael@0: a.recycle(); michael@0: michael@0: mCompoundPadding = getCompoundDrawablePadding(); michael@0: } michael@0: michael@0: /** michael@0: * {@inheritDoc} michael@0: */ michael@0: @Override michael@0: public void onDraw(Canvas canvas) { michael@0: int width = getMeasuredWidth(); michael@0: michael@0: // Layout doesn't return a proper width for getWidth(). michael@0: // Instead check the width of the first line, as we've restricted to just one line. michael@0: if (getLayout().getLineWidth(0) > width) { michael@0: final Drawable leftDrawable = getCompoundDrawables()[0]; michael@0: int drawableWidth = 0; michael@0: if (leftDrawable != null) { michael@0: drawableWidth = leftDrawable.getIntrinsicWidth() + mCompoundPadding; michael@0: width -= drawableWidth; michael@0: } michael@0: michael@0: int color = getCurrentTextColor(); michael@0: float stop = ((float) (width - mFadeWidth) / (float) width); michael@0: LinearGradient gradient = new LinearGradient(0, 0, width, 0, michael@0: new int[] { color, color, 0x0 }, michael@0: new float[] { 0, stop, 1.0f - (drawableWidth / width) }, michael@0: Shader.TileMode.CLAMP); michael@0: getPaint().setShader(gradient); michael@0: } else { michael@0: getPaint().setShader(null); michael@0: } michael@0: michael@0: // Do a default draw. michael@0: super.onDraw(canvas); michael@0: } michael@0: }