java - 如何在 Canvas 上绘制全部大写的文本

标签 java android android-canvas android-paint

我正在使用 TextPaintStaticLayout 在我的 Canvas 上绘制文本。但是,我希望我的文字以大写字母绘制。在线建议是使用 toUpperCase(),但该更改不会反射(reflect)在 Canvas 上。

这是我的代码:

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

最佳答案

toUpperCase() 不会修改原始字符串,它会创建并返回新字符串。您忽略了 toUpperCase() 的结果,这就是它对您不起作用的原因。

topText.toUpperCase(); 替换为 topText = topText.toUpperCase();bottomText

相同

关于java - 如何在 Canvas 上绘制全部大写的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55096463/

相关文章:

android - RemoteViews (Widget) 在使用位图时有模糊文本

java - 使用 JDK 7u7 构建的 Applet,在安装的 JRE 7u72 上运行 - 两者之间是否存在安全问题风险?

java - 为什么等于功能不起作用?

android - 确定 View 是否在屏幕上 - Android

android - Android Studio-手动运行注释处理器

Android,如何从 Canvas 获取像素颜色?

java - 将父类(super class)类型转换为子类类型?

java - 有没有办法在不初始化的情况下创建原始数组?

android - Google 登录错误 : Status Code: SIGN_IN_REQUIRED when signing in with Google on android

java - Android保存坐标 Canvas 绘制圆并在不同设备上显示