Paint属性不适用于Canvas

问题描述 投票:-2回答:1

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

enter image description here

这是我的代码:

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

UPDATE

我已经评论过了

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

java android android-canvas android-paint
1个回答
2
投票

为了获得你想要的行为,你只需删除第二个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

© www.soinside.com 2019 - 2024. All rights reserved.