android – 如何勾勒出TextView?

前端之家收集整理的这篇文章主要介绍了android – 如何勾勒出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,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);
}
}
最佳答案
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"

猜你在找的Android相关文章