如何勾勒出一个TextView? [英] How to outline a TextView?

查看:143
本文介绍了如何勾勒出一个TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

什么是我的输出:

哪里是我的codeS?

 公共类TypeFaceTextView扩展TextView的{

私人静电漆getWhiteBorderPaint(){
    涂料P =新的油漆(Color.WHITE);
    返回磷;
}

私有静态最后的油漆BLACK_BORDER_PAINT = getWhiteBorderPaint();

静态{
    BLACK_BORDER_PAINT.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
}

@覆盖
公共无效的setText(CharSequence的文字,BufferType型){

    super.setText(的String.Format(text.toString()),类型);
}

私有静态最终诠释BORDER_WIDTH = 1;

私人字样字样;

公共TypeFaceTextView(上下文的背景下){
    超(上下文);
}

公共TypeFaceTextView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);

    setDrawingCacheEnabled(假);

    setTypeface(ATTRS);
}

私人无效setTypeface(AttributeSet中的ATTRS){
    最后弦乐typefaceFileName = attrs.getAttributeValue(NULL,字体);
    如果(typefaceFileName!= NULL){
        字体= Typeface.createFromAsset(的getContext()getAssets(),typefaceFileName);
    }

    setTypeface(字体);
}

公共TypeFaceTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);

    setTypeface(ATTRS);
}

@覆盖
公共无效画(油画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);

}

私人无效drawBackground(帆布aCanvas,诠释ADX,诠释ADY){
    aCanvas.translate(ADX,ADY);
    super.draw(aCanvas);
}
}
 

解决方案

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

 公共类YourTextView扩展的TextView {.........
 

2)执行此操作在其绘制方法

  @覆盖
公共无效画(油画画布){
        的for(int i = 0;我小于5;我++){
        super.draw(画布);
    }
}
 

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

 安卓则shadowColor =@色/白
机器人:shadowRadius =5
 

What I want to do? (blue will be changed as white)

What I did?
I have found a class which extends TextView that able to outline textview very close to what I want. The problem is that I could not change stroke color to any color, it draws always as black. How to set border color as white?

What is my output:

Where are my codes?

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

解决方案

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"

这篇关于如何勾勒出一个TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆