如何勾勒出一个TextView?(How to outline a TextView?)

2019-07-20 13:40发布

我想做的事? (蓝色将被改变为白色)

我做了什么?
我发现它扩展TextView的是能够勾勒出的TextView非常接近我想要的一类。 问题是,我不能笔触颜色更改为任何颜色,它总是绘制为黑色。 如何设置边框颜色为白色?

什么是我的输出:

哪里是我的代码?

public class TypeFaceTextView extends TextView {

private static Paint getWhiteBorderPaint(){
    Paint p = new Paint(Color.WHITE);
    return p;
}

private static final Paint BLACK_BORDER_PAINT = getWhiteBorderPaint();

static {
    BLACK_BORDER_PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
}

@Override
public void setText(CharSequence text, BufferType type) {

    super.setText(String.format(text.toString()), type);
}

private static final int BORDER_WIDTH = 1;

private Typeface typeface;

public TypeFaceTextView(Context context) {
    super(context);
}

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

    setDrawingCacheEnabled(false);

    setTypeface(attrs);
}

private void setTypeface(AttributeSet attrs) {
    final String typefaceFileName = attrs.getAttributeValue(null, "typeface");
    if (typefaceFileName != null) {
        typeface = Typeface.createFromAsset(getContext().getAssets(), typefaceFileName);
    }

    setTypeface(typeface);
}

public TypeFaceTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    setTypeface(attrs);
}

@Override
public void draw(Canvas aCanvas) {
    aCanvas.saveLayer(null, BLACK_BORDER_PAINT, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);

    drawBackground(aCanvas, -BORDER_WIDTH, -BORDER_WIDTH);
    drawBackground(aCanvas, BORDER_WIDTH + BORDER_WIDTH, 0);
    drawBackground(aCanvas, 0, BORDER_WIDTH + BORDER_WIDTH);
    drawBackground(aCanvas, -BORDER_WIDTH - BORDER_WIDTH, 0);

    aCanvas.restore();
    super.draw(aCanvas);

}

private void drawBackground(Canvas aCanvas, int aDX, int aDY) {
    aCanvas.translate(aDX, aDY);
    super.draw(aCanvas);
}
}

Answer 1:

1)创建TextView的对象扩展的TextView

public class YourTextView extends TextView { .........

2)做这在其绘制方法

@Override
public void draw(Canvas canvas) {
        for (int i = 0; i < 5; i++) {
        super.draw(canvas);
    }
}

3)设置的TextView的XML侧面,如下

android:shadowColor="@color/white"
android:shadowRadius="5"


Answer 2:

不能这个这个,但尝试尝试:PorterDuff.Mode

http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html

尝试将其更改为“添加”或“清除”,希望这有助于。



Answer 3:

你需要改变你的getWhiteBorderPaint()方法如下:

private static Paint getWhiteBorderPaint(){
    Paint p = new Paint();
    p.setColor(Color.WHITE);
    return p;
}

该涂料的构造只需bitmasked标志,并且不支持任意整数作为参数。



Answer 4:

Investigated into the original problem stated by this question. Found the solution.

First, change DST_OUT to DARKEN

static {
    BLACK_BORDER_PAINT.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
}

Secondly, save the original text color, and put the intended outline color up, draw the outline, and then restore the original text color.

@Override
public void draw(Canvas aCanvas) {
    int originalColor = this.getCurrentTextColor();
    this.setTextColor(0xff000000); //set it to white.

    aCanvas.saveLayer(null, borderPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);

        drawBackground(aCanvas, -BORDER_WIDTH, -BORDER_WIDTH);
        drawBackground(aCanvas, BORDER_WIDTH + BORDER_WIDTH, 0);
        drawBackground(aCanvas, 0, BORDER_WIDTH + BORDER_WIDTH);
        drawBackground(aCanvas, -BORDER_WIDTH - BORDER_WIDTH, 0);

    this.setTextColor(originalColor);
    aCanvas.restore();
    super.draw(aCanvas);
}


Answer 5:

我发现简单的方法,大纲视图而不TextView的继承。 我曾写道,使用Android的Spannable概括为简单的文本库。 该解决方案提供了可能性大纲文本的一部分。

图书馆: OutlineSpan

类(可以只复制类): OutlineSpan



文章来源: How to outline a TextView?