繁体   English   中英

如何绘制具有不同笔触和填充颜色的文本?

[英]How to draw text with different stroke and fill colors?

我想在我的应用程序中显示如下文本。 我正在使用样式为FILL_AND_STROKE Paint类来实现这一点。 但是只有一种方法setColor()可用于设置颜色。

如何设置不同的笔触和填充颜色?

具有不同笔触和填充颜色的文本

在自定义 TextView 中(在 EditText 中不起作用):

@Override
public void onDraw(Canvas canvas)
{
    final ColorStateList textColor = getTextColors();

    TextPaint paint = this.getPaint();

    paint.setStyle(Style.STROKE);
    paint.setStrokeJoin(Join.ROUND);
    paint.setStrokeMiter(10);
    this.setTextColor(strokeColor);
    paint.setStrokeWidth(strokeWidth);

    super.onDraw(canvas);
    paint.setStyle(Style.FILL);

    setTextColor(textColor);
    super.onDraw(canvas);
}

不要使用 FILL_AND_STROKE。 使用 FILL 绘制一次,然后更改颜色并使用 STROKE 绘制。

(这适用于矩形。我不确定 STROKE 是否适用于文本。您必须尝试并找出答案。)

我使用上面的第一个解决方案来提出这个想法:放下一个更大的 STROKE 文本,然后用一个较小的 FILL_AND_STROKE 文本覆盖它:

mScorePaint = new TextPaint();
mScorePaint.setTextSize(63);
mScorePaint.setStyle(Style.STROKE);
mScorePaint.setStrokeJoin(Join.ROUND);
mScorePaint.setStrokeMiter(10.0f);
mScorePaint.setStrokeWidth(frameWidth/50.0f); // about 12
mScorePaint.setColor(0xffff0000); // black

c.drawText(Integer.toString(mScore), x, y, mScorePaint);  // red first

mScorePaint.setStrokeWidth(frameWidth/125.0f); // about 5
mScorePaint.setColor(0xff000000); // red

c.drawText(Integer.toString(mScore), x, y, mScorePaint);  // black on top

因为单独的 FILL 没有看到任何 Stroke 属性,而且非常薄。

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.Join
import android.graphics.Rect
import android.util.AttributeSet
import android.widget.TextView

@SuppressLint("AppCompatCustomView")
class BorderTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextView(context, attrs, defStyleAttr) {
    private var strokeWidth: Float = 0F
    private val paintStroke = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        paint.color = currentTextColor
        paint.typeface = typeface

        if (attrs != null) {
            val a = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView)
            if (a.hasValue(R.styleable.BorderTextView_strokeColor)) {
                strokeWidth =
                    a.getDimensionPixelSize(R.styleable.BorderTextView_strokeWidth, 1).toFloat()
                val strokeColor = a.getColor(R.styleable.BorderTextView_strokeColor, 0)
                val strokeMiter =
                    a.getDimensionPixelSize(R.styleable.BorderTextView_strokeMiter, 10).toFloat()
                var strokeJoin: Join? = null
                when (a.getInt(R.styleable.BorderTextView_strokeJoinStyle, 2)) {
                    0 -> strokeJoin = Join.MITER
                    1 -> strokeJoin = Join.BEVEL
                    2 -> strokeJoin = Join.ROUND
                }
                setStroke(strokeWidth, strokeColor, strokeJoin, strokeMiter)
            }
            a.recycle()
        }
    }

    private fun setStroke(width: Float, color: Int, join: Join?, miter: Float) {
        paintStroke.strokeJoin = join
        paintStroke.strokeMiter = miter
        paintStroke.strokeWidth = width
        paintStroke.style = Paint.Style.STROKE
        paintStroke.color = color
        paintStroke.textSize = textSize
        paintStroke.typeface = typeface
        paintStroke.letterSpacing = letterSpacing
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        this.setMeasuredDimension(measuredWidth + strokeWidth.toInt(), measuredHeight);
    }
    override fun onDraw(canvas: Canvas) {
        val r = Rect()
        paint.getTextBounds(text.toString(), 0, text.length, r)
        val desc = paint.descent()
        val asc = paint.ascent()
        val y = (height.toFloat() - (1 + asc + desc / 2F)) / 2F
        val x = width / 2f - r.width() / 2f - r.left
        canvas.drawText(text.toString(), x, y, paintStroke)
        canvas.drawText(text.toString(), x, y, paint)
    }
}

不完全确定,但也许你可以使用这个:

关联

 TextView test = (TextView) findViewById(R.id.test);

 test.setShadowLayer(float, float, float, int);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM