java - 绘制属性在应用两次时不起作用

标签 java android android-canvas android-paint staticlayout

我正在尝试创建一种具有白色和黑色轮廓的自定义影响字体(又名“模因字体”)。我在 Canvas 的两端都有 2 个文本,但只有其中一个反射(reflect)了更改。这是到目前为止我所拥有的:

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.getHeigth() - 210);
        bottomStrokeLayout.draw(canvas);

最佳答案

您传递来绘制bottomStrokeLayout 和bottomFillLayout 的坐标存在差异。

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

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

关于java - 绘制属性在应用两次时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55099847/

相关文章:

java - Android: fragment 生命周期中的方法

java - 如何在java中以编程方式向列表Double添加元素

Java Maven Jasperreport 从 Eclipse 运行 好的,从 java - jar JRRuntimeException : Chart theme "eye.candy.sixties" not found

android - 将布局划分为 android 中的图 block 的最佳实践?

java - Android:使用 postInvalidate() 还是直接调用 onDraw?

android - View 不适合 Canvas 比例的 Canvas

java - 将 Actionscript/Flex Air Android 项目与 Java Android 项目相结合

java - Android ConnectivityManager NetworkCallback 崩溃

java - 使用Android相机检测条形码

android - 如何使用 canvas.. 在 onDraw() 中使用 path() 设置起点?