如何在Canvas上的所有大写字母中绘制文本

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

我使用CanvasTextPaint在我的StaticLayout上绘制文字。但是,我希望我的文字用大写字母绘制。网上的建议是使用toUpperCase(),但这种变化并没有反映在画布上。

这是我的代码:

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

        File file;
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath();
        file = new File(path + "/SimpliMeme/" + timeStamp + "-" + counter + ".jpg");
        file.getParentFile().mkdir();

        try {
            OutputStream stream = new FileOutputStream(file);
            mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            stream.flush();
            stream.close();
            Toast.makeText(getContext(), "Top Text: " + String.valueOf(topTextMeasurement) + " and bottom text: " + String.valueOf(bottomTextMeasurement),
                    Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
    }
java android android-canvas android-paint
1个回答
3
投票

toUpperCase()不会修改原始字符串,它会创建并返回新字符串。你忽略了toUpperCase()的结果,这就是为什么它不适合你。

topText.toUpperCase();替换topText = topText.toUpperCase();bottomText也是如此

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