如何在画布上以所有大写字母绘制文本

我正在绘制文本在我的使用和.但是,我希望我的文本以大写字母绘制。在线建议是使用,但该更改不会反映在画布上。CanvasTextPaintStaticLayouttoUpperCase()


这是我的代码:


public void createBitmapAndSave(ImageView img) {


        BitmapDrawable bitmapDrawable = ((BitmapDrawable) img.getDrawable());

        Bitmap bitmap = bitmapDrawable.getBitmap();

        Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);


        String topText = topTextView.getText().toString();

        String bottomText = bottomTextView.getText().toString();


        Canvas canvas = new Canvas(mutableBitmap);

        TextPaint topPaint = new TextPaint();

        TextPaint bottomPaint = new TextPaint();

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


        topPaint.setColor(Color.WHITE);

        topPaint.setStyle(Paint.Style.FILL);

        topPaint.setTextSize(topTextView.getTextSize());

        topPaint.setTypeface(typeface);


        bottomPaint.setColor(Color.WHITE);

        bottomPaint.setStyle(Paint.Style.FILL);

        bottomPaint.setTextSize(bottomTextView.getTextSize());

        bottomPaint.setTypeface(typeface);


        float topTextMeasurement = topPaint.measureText(topText);

        float bottomTextMeasurement = bottomPaint.measureText(bottomText);


        StaticLayout topLayout = new StaticLayout(topText, topPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1.0f,

                0.0f, false);

        StaticLayout bottomLayout = new StaticLayout(bottomText, bottomPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER,

                1.0f, 0.0f, false);


        topText.toUpperCase();

        bottomText.toUpperCase();


        canvas.translate(0,0);

        topLayout.draw(canvas);


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

        bottomLayout.draw(canvas);

        counter++;


茅侃侃
浏览 69回答 1
1回答

慕田峪7331174

toUpperCase()不修改原始字符串,而是创建并返回新字符串。你忽略了 的结果,这就是为什么它不适合你。toUpperCase()替换为 。相同topText.toUpperCase();topText = topText.toUpperCase();bottomText
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java