在 Android 中,可以使用 Canvas 和 Paint 来绘制竖直的文本。以下是一个示例代码:

public class VerticalTextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new VerticalTextView(this));
    }

    private class VerticalTextView extends View {
        private Paint paint;

        public VerticalTextView(Context context) {
            super(context);
            paint = new Paint();
            paint.setTextSize(50);
            paint.setColor(Color.BLACK);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            String text = 'Hello World';
            int x = 50;
            int y = 50;

            for (int i = 0; i < text.length(); i++) {
                canvas.drawText(String.valueOf(text.charAt(i)), x, y, paint);
                y += paint.getTextSize();
            }
        }
    }
}

在上述示例中,我们创建了一个自定义的 View 类 VerticalTextView,继承自 View 类。在构造函数中,我们初始化了一个 Paint 对象,并设置了文本的大小和颜色。

在 onDraw 方法中,我们使用一个循环来逐个绘制文本中的字符。在每次绘制时,我们使用 drawText 方法来绘制一个字符,并将 y 坐标递增,以实现竖直排列的效果。

最后,我们将 VerticalTextView 对象设置为 Activity 的主要布局。运行应用程序时,将会显示一个包含竖直排列文本的画布。

Android 绘制竖直文本:代码示例及详解

原文地址: https://www.cveoy.top/t/topic/qk05 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录