Paint 属性不适用于 Canvas

Paint attributes are not working on Canvas

我正在尝试创建带有黑色轮廓的白色效果字体(又名 "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); 并绘制了黑色边框。所以绘制填充文本时,要么填充文本覆盖了轮廓,要么轮廓不存在。

To get the behavior you want, you'd just remove the second canvas.translate(0, canvas.getHeight() - 210);.

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

有关详细信息,请参阅 the android.graphics.Canvas documentation of the translate method