簡體   English   中英

如何在 TextView 外部添加行程

[英]How to add stroke on outside of TextView

歡迎任何想法,代碼片段!

我創建了一個擴展AppCompatTextViewCustomTextView class ,我這樣做是為了向無聊TextView添加筆畫支持。 問題是, Paint.Style.STROKETextView的內部添加了描邊。 應該有一些東西可以讓我們在外筆畫和內筆畫之間進行選擇。

PS:如果需要,我可以分享完整的CustomTextView class,沒什么大不了的。

  • 我們目前擁有的:
    在此處輸入圖像描述
  • 我們想要達到的目標:
    在此處輸入圖像描述

這是我們的CustomTextView中的onDraw方法,用於將筆畫添加到 textView。

    @Override
    protected void onDraw(Canvas canvas) {
        if(_strokeWidth > 0) {
            //set paint to fill mode
            Paint p = getPaint();
            p.setStyle(Paint.Style.FILL);
            //draw the fill part of text
            super.onDraw(canvas);
            //save the text color
            int currentTextColor = getCurrentTextColor();
            //set paint to stroke mode and specify
            //stroke color and width
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(_strokeWidth);
            setTextColor(_strokeColor);
            //draw text stroke
            super.onDraw(canvas);
            //revert the color back to the one
            //initially specified
            setTextColor(currentTextColor);
        } else {
            super.onDraw(canvas);
        }
    }

下面繪制了TextView中字符的輪廓,但注意將字符本身剪掉,以免它們被覆蓋。

OutlineTextView.java

public class OutlineTextView extends androidx.appcompat.widget.AppCompatTextView {
    private final Paint mOutlinePaint = new Paint();
    private final Path mOutlinePath = new Path();
    private float mStrokeWidth = 0f;

    public OutlineTextView(Context context) {
        super(context);
        init();
    }

    public OutlineTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public OutlineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mOutlinePaint.setStrokeWidth(0f);
        mOutlinePaint.setStyle(Paint.Style.STROKE);
        mOutlinePaint.setColor(Color.RED);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        float xOffset = getLayout().getLineLeft(0) + getPaddingLeft();
        float baseline = getLayout().getLineBaseline(0) + getPaddingTop();
        getPaint().getTextPath(getText().toString(), 0, getText().length(), xOffset, baseline, mOutlinePath);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mStrokeWidth > 0) {
            canvas.save();
            // The following insures that we don't draw inside the characters.
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                canvas.clipPath(mOutlinePath, Region.Op.DIFFERENCE);
            } else {
                canvas.clipOutPath(mOutlinePath);
            }
            canvas.drawPath(mOutlinePath, mOutlinePaint);
            canvas.restore();
        }
    }

    public void setStrokeWidth(Float strokeWidth) {
        mStrokeWidth = strokeWidth;
        mOutlinePaint.setStrokeWidth(strokeWidth);
        invalidate();
    }
}

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM