gpt4 book ai didi

android - 如何勾画一个TextView?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:46 26 4
gpt4 key购买 nike

我想做什么?(蓝色会变成白色) enter image description here

我做了什么?
我找到了一个扩展 TextView 的类,它能够非常接近我想要的轮廓 textview。问题是我无法将描边颜色更改为任何颜色,它始终绘制为黑色。如何设置边框颜色为白色?

我的输出是什么:
enter image description here

我的代码在哪里?

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

最佳答案

我找到了无需继承 TextView 即可勾勒 View 轮廓的简单方法。我写了一个简单的库,使用 Android 的 Spannable 来概述文本。该解决方案提供了仅概述部分文本的可能性。

图书馆:OutlineSpan

类(class)(只能复制类(class)):OutlineSpan

关于android - 如何勾画一个TextView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15091790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com