How to outline a TextView?

 ̄綄美尐妖づ 提交于 2019-11-28 11:34:24

1) create your textview object extends TextView

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

2) Do this on its draw method

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

3) set textview's xml side as below

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

Couldn't this this but try experimenting with: PorterDuff.Mode

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

Try changing it to 'ADD' or 'CLEAR', hope this helps.

You need to change your getWhiteBorderPaint() method to the following:

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

The Paint constructor only takes bitmasked flags and doesn't support arbitrary ints as parameters.

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);
}

I found simple way to outline view without inheritance from TextView. I had wrote simple library that use Android's Spannable for outlining text. This solution gives possibility to outline only part of text.

Library: OutlineSpan

Class (you can copy only class):OutlineSpan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!