如何淡出TextView中最后一行的结尾?

时间:2018-03-15 09:21:13

标签: android android-layout textview

我们怎样才能在class LogExportingProcess extends Thread { public void run() { //i want to change the value of the percent complete variable in here. } } 的最后一行实现淡出效果,就像Play商店应用中的“WHAT'S NEW”部分一样?

Screenshot of Play Store app

1 个答案:

答案 0 :(得分:6)

我们可以通过继承TextView并模仿View对褪色边缘的作用,相对简单地完成淡化效果,但仅限于最后一行的最后一段。

在这个例子中,我们基本上创建了一个从透明到纯黑的线性渐变。这将根据TextView的最终行长度进行缩放,然后使用PorterDuff.Mode.DST_OUT在适当的区域绘制,这基本上将基础内容清除到相对于渐变的程度&#39 ;在给定点上的不透明度。

public class FadingTextView extends TextView {

    private static final int FADE_LENGTH_FACTOR = 3;

    private final Shader shader;
    private final Matrix matrix;
    private final Paint paint;
    private final Rect bounds;

    public FadingTextView(Context context) {
        this(context, null);
    }

    public FadingTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public FadingTextView(Context context, AttributeSet attrs, int defStyleAttribute) {
        super(context, attrs, defStyleAttribute);

        matrix = new Matrix();
        paint = new Paint();
        bounds = new Rect();
        shader = new LinearGradient(0f, 0f, 1f, 0f, 0, 0xFF000000, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Locals
        final Matrix m = matrix;
        final Rect b = bounds;
        final Layout l = getLayout();

        // Last line index
        final int line = getLineCount() - 1;

        // Determine text direction
        final boolean isRtl = l.getParagraphDirection(line) == Layout.DIR_RIGHT_TO_LEFT;

        // Last line bounds
        getLineBounds(line, b);

        // Adjust end bound to text length
        final int lineStart = l.getLineStart(line);
        final int lineEnd = l.getLineEnd(line);
        final CharSequence text = getText().subSequence(lineStart, lineEnd);
        final int measure = (int) (getPaint().measureText(text, 0, text.length()) + .5f);
        if (isRtl) {
            b.left = b.right - measure;
        }
        else {
            b.right = b.left + measure;
        }

        // Figure fade length
        final int fadeLength = b.width() / FADE_LENGTH_FACTOR;

        // Adjust start bound to fade start
        if (isRtl) {
            b.right = b.left + fadeLength;
        }
        else {
            b.left = b.right - fadeLength;
        }

        // Save the layer
        final int saveCount = canvas.saveLayer(b.left, b.top, b.right,
                                               b.bottom, null,
                                               Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);

        // Let TextView draw itself
        super.onDraw(canvas);

        // Adjust and set the Shader Matrix
        m.reset();
        m.setScale(fadeLength, 1f);
        if (isRtl) {
            m.postRotate(180f, fadeLength / 2f, 0f);
        }
        m.postTranslate(b.left, 0f);
        shader.setLocalMatrix(matrix);

        // Finally draw the fade
        canvas.drawRect(b, paint);

        canvas.restoreToCount(saveCount);
    }
}

这是TextView的替代品,您可以在布局中使用它。

<com.mycompany.myapp.FadingTextView
    android:layout_width="275dp"
    android:layout_height="wrap_content"
    android:background="#e2f3eb"
    android:textColor="#0b8043"
    android:text="..." />

screenshot

注意:

  • 渐变长度计算基于最终行长度的常量分数,此处由FADE_LENGTH_FACTOR确定。这似乎与Play商店组件的基本方法相同,因为淡入淡出的绝对长度似乎随着行长度而变化。 FADE_LENGTH_FACTOR值可以根据需要进行更改。

  • 如果您正在使用appcompat库及其便利设施,则此类应扩展AppCompatTextView,以确保正确处理着色和诸如此类的内容。