java - 如何在 Canvas 上水平居中文本

标签 java android android-canvas textview android-paint

我有一个带有 2 个 TextView 的 ImageView。我正在尝试将字幕的中位数与 Canvas 的中位数(宽度/2)对齐。这是我要实现的目标的说明:

https://imgur.com/a/7fklSBv

到目前为止,我尝试将 TextView 从其左端与 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);
        Paint topPaint = new Paint();
        Paint bottomPaint = new Paint();

        topPaint.setColor(Color.BLUE);
        topPaint.setStyle(Paint.Style.FILL);
        topPaint.setShadowLayer(10f, 10f, 10f, Color.BLACK);
        topPaint.setTextSize(topTextView.getTextSize());

        bottomPaint.setColor(Color.RED);
        bottomPaint.setStyle(Paint.Style.FILL);
        bottomPaint.setShadowLayer(10f, 10f, 10f, Color.BLACK);
        bottomPaint.setTextSize(bottomTextView.getTextSize());

        canvas.drawText(topText, (canvas.getWidth()) / 2, 200, topPaint);
        canvas.drawText(bottomText, (canvas.getWidth()) / 2, canvas.getHeight() - 200, bottomPaint);

        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(), "Meme Saved", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Uri contentUri = Uri.fromFile(file);
        mediaScanIntent.setData(contentUri);
        Objects.requireNonNull(getContext()).sendBroadcast(mediaScanIntent);
        counter++;
    }

最佳答案

其实很简单。您需要做的就是使用 Paint.measureText() 方法获取文本的宽度,除以 2 得到它的一半,然后将其向左移动使其居中。

看看这个。我创建了两个 float 变量来保存 Canvas 上每个文本的宽度:

float topTextMeasurement = topPaint.measureText(topText);
float bottomTextMeasurement = bottomPaint.measureText(bottomText);

然后我在您的 Canvas.drawText() 方法的 x 参数中完成了上述调整。

canvas.drawText(topText, topX - (topTextMeasurement/2), 200, topPaint);
canvas.drawText(bottomText, bottomX - (bottomTextMeasurement/2), canvas.getHeight() - 200, bottomPaint);

但这仅适用于您的文本不会超过一行的情况。对于多行文本,我建议您查看 DynamicLayout

关于java - 如何在 Canvas 上水平居中文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54997325/

相关文章:

android - 在 Android 中用透明颜色填充 Canvas

java - 搜索 .jar 文件 eclipse

java - 如何使用 JPanel 将一系列线函数合并为一个 drawPolygon 函数?

java - 使用 Activiti 工作流程跳过或完成用户任务

java - 同一项目中的多个 android-support-v4 版本

使用 Canvas 的 Android 弹跳球

java - 似乎无法让 RemoteIpFilter (https) 与 spring webflow 正确交互

Android获取GPU型号

java - Android 谷歌地图 - 如何将谷歌地图路线分成相等的部分?

Android:如何做这个框架涂料?