-2

我正在尝试创建带有黑色轮廓的白色影响字体(又名“The Meme Font”)。我为在 Canvas 上绘制的两个文本应用了逻辑,但它仅适用于其中一个。这是显示我在说什么的结果:

在此处输入图像描述

这是我的代码:

        Canvas canvas = new Canvas(mutableBitmap);

        TextPaint topFillPaint = new TextPaint();
        TextPaint bottomFillPaint = new TextPaint();

        TextPaint topStrokePaint = new TextPaint();
        TextPaint bottomStrokePaint = new TextPaint();

        Typeface typeface = getResources().getFont(R.font.impact);

        topFillPaint.setColor(Color.WHITE);
        topFillPaint.setTextSize(topTextView.getTextSize());
        topFillPaint.setTypeface(typeface);

        topStrokePaint.setStyle(Paint.Style.STROKE);
        topStrokePaint.setStrokeWidth(8);
        topStrokePaint.setColor(Color.BLACK);
        topStrokePaint.setTextSize(topTextView.getTextSize());
        topStrokePaint.setTypeface(typeface);

        bottomFillPaint.setColor(Color.WHITE);
        bottomFillPaint.setTextSize(bottomTextView.getTextSize());
        bottomFillPaint.setTypeface(typeface);

        bottomStrokePaint.setStyle(Paint.Style.STROKE);
        bottomStrokePaint.setStrokeWidth(8);
        bottomStrokePaint.setColor(Color.BLACK);
        bottomStrokePaint.setTextSize(bottomTextView.getTextSize());
        bottomStrokePaint.setTypeface(typeface);

        float topTextMeasurement = topFillPaint.measureText(topText);
        float bottomTextMeasurement = bottomFillPaint.measureText(bottomText);

        StaticLayout topFillLayout = new StaticLayout(topText, topFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);
        StaticLayout topStrokeLayout = new StaticLayout(topText, topStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);


        StaticLayout bottomFillLayout = new StaticLayout(bottomText, bottomFillPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);
        StaticLayout bottomStrokeLayout = new StaticLayout(bottomText, bottomStrokePaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,
                1.0f, 0.0f, false);

        canvas.translate(0,0);
        topFillLayout.draw(canvas);

        canvas.translate(0,0);
        topStrokeLayout.draw(canvas);

        canvas.translate(0, canvas.getHeight() - 210);
        bottomFillLayout.draw(canvas);

        canvas.translate(0, canvas.getHeight() - 210);
        bottomStrokeLayout.draw(canvas);

更新

我已经注释掉了

canvas.translate(0, canvas.getHeight() - 210);并且bottomFillLayout.draw(canvas); 绘制了黑色边框。因此,填充文本覆盖轮廓或绘制填充文本时轮廓不存在。

4

1 回答 1

2

要获得您想要的行为,您只需删除第二个canvas.translate(0, canvas.getHeight() - 210);.

这些canvas.translate调用会调整 Canvas 的当前翻译(它会添加到翻译中,但不会绝对重置它)。这意味着这canvas.translate(0, 0);实际上是一个空操作,因为它根本不会改变翻译(这些行可以被删除)。绘制调用后翻译不会重置,因此这意味着您的第二个canvas.translate(0, canvas.getHeight() - 210);调用正在从屏幕上平移(除非您的屏幕高度小于210 * 2)。

有关更多信息,请参阅translate 方法的 android.graphics.Canvas 文档

于 2019-03-12T22:26:15.683 回答