当前位置: 首页 > news >正文

Android 文字垂直排列,文字向右旋转90度

public class VerticalTextView extends View {private final int ROTATION_ANGLE = 90; // 旋转角度,用于将文本垂直排列private String text; // 要显示的文本private TextPaint textPaint; // 用于绘制文本的画笔private Rect textBounds;// 文本边界float x, y;// 文本的绘制位置int width, height; / View 的宽度和高度private Context mContext;public VerticalTextView(Context context) {super(context);this.mContext = context;init();}public VerticalTextView(Context context, AttributeSet attrs) {super(context, attrs);this.mContext = context;init();}public VerticalTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);this.mContext = context;init();}private void init() {textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);}
// 获取文本的边界信息public void setText(String text, float size) {this.text = text;textBounds = new Rect();textPaint.setTextSize(size);textPaint.setColor(Color.parseColor("#333333"));textPaint.getTextBounds(text, 0, text.length(), textBounds);requestLayout(); // 请求重新布局postInvalidate();// 请求重新绘制}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//将文本高度与左内边距和右内边距相加,计算出视图的宽度width = textBounds.height() + getPaddingLeft() + getPaddingRight();//将文本宽度与上内边距和下内边距相加,计算出视图的高度。height = textBounds.width() + getPaddingTop() + getPaddingBottom();//设置测量结果setMeasuredDimension(width, height);}@Overrideprotected void onDraw(Canvas canvas) {//保存当前的画布状态,以便后续的操作不会影响到其他绘制。canvas.save();//使文本垂直排列,旋转了90度canvas.rotate(ROTATION_ANGLE, getWidth() / 2f, getHeight() / 2f);if (null != textBounds) {//文字水平居中x = (getWidth() - textBounds.width()) / 2f - textBounds.left; //文字垂直居中y = (getHeight() + textBounds.height()) / 2f - textBounds.bottom;if (null != text) {//绘制文字canvas.drawText(text, x, y, textPaint);//恢复之前保存的画布状态,以确保后续的绘制操作不受旋转的影响。canvas.restore();}}}
}
<com.signature.view.VerticalTextViewandroid:id="@+id/tvInfoNotice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="3dp"android:background="@color/white" />

http://www.lryc.cn/news/273049.html

相关文章:

  • 域名授权验证系统PHP源码,盗版追踪、双重授权和在线加密功能,附带安装教程
  • Qt线程封装FFmpeg播放器类
  • git 常用命令 修改 远程仓库 默认分支
  • springboot项目禁用dataSource数据源功能,只需修改yml文件,关闭数据库连接功能
  • 【Vue.js设计与实现解读-1】
  • 苗情生态自动监测系统-科普知识
  • test 系统学习-04-test converate 测试覆盖率 jacoco 原理介绍
  • 小型企业成为网络犯罪分子获取数据的目标
  • PyTorch的Tensor(张量)
  • spug发布问题汇总记录
  • SpringBoot-搭建集成Mybatis的项目
  • mysql隐式转换规则
  • 怎么解决 Nginx反向代理加载速度慢?
  • Eureka工作原理超详细讲解介绍
  • SQL WHERE 语句(条件选择)
  • 用UCLI(TCL)控制verdi dump 波形
  • 如何使用 Python+selenium 进行 web 自动化测试?
  • 约瑟夫问题
  • 文件管理方法:利用文件大小进行筛选,高效移动文件至目标文件夹
  • python报错:TypeError: Descriptors cannot be created directly.
  • Linux 内核调试
  • Prometheus-AlertManager 邮件告警
  • Volcano Controller控制器源码解析
  • 开源协议简介和选择
  • 大创项目推荐 深度学习卫星遥感图像检测与识别 -opencv python 目标检测
  • pod的环节
  • Unity | Shader基础知识番外(向量数学知识速成)
  • 一个小白的微不足道的见解关于未来
  • 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
  • 抖店做不起来?新手常见起店失败问题总结,看下你中了几条?