android 绘制斜体文本的一种方式

直接给 TextPaint设置斜体 然后通过 StaticLayout绘制也可以绘制斜体。但是问题是 StaticLayout直接绘制有时候文本会被裁切,也就是绘制范围没法精准控制。

js 复制代码
Rect bounds = new Rect();
mTextPaint.getTextBounds(c,0,c.length(),bounds);
canvas.save();
canvas.translate(bounds.width()/2f, bounds.height()/2f); // 移动到你想要的中心点
canvas.skew(-0.3f, 0);            // 倾斜
canvas.translate(-bounds.width()/2f, -bounds.height()/2f); // 移回原位置
StaticLayout itemStaticLayout = new StaticLayout(c, mTextPaint, txtWidth,
Layout.Alignment.ALIGN_NORMAL, 1.0F, 0, false);
itemStaticLayout.draw(canvas);
canvas.restore();

计算文本倾斜之后的宽度

js 复制代码
float skewX = 0.3f;
float w = paint.measureText(text);
float h = bounds.height();
float skewWidth = w + h * Math.abs(skewX);

// 让文字居中显示(考虑倾斜后的宽度)
float startX = (canvasWidth - skewWidth) / 2;

canvas.save();
canvas.translate(pivotX, pivotY);
canvas.skew(skewX, 0);
canvas.translate(-pivotX, -pivotY);
canvas.drawText(text, startX, baselineY, paint);
canvas.restore();
相关推荐
天空之城--6 小时前
Android逆向安全合规接单指南
android·安全
消失的旧时光-19438 小时前
Android 系统层扫盲 09:AMS、ATMS、WMS、PMS 到底分别管什么?
android·wms·pms·ams·aosp·atms
Godikov8 小时前
Android 系统级设备应用踩坑实录:sharedUserId 签名、SDK 授权失败 -4、开机自启与 U 盘 OTA 升级
android
IT毕设实战小研11 小时前
基于大数据的国内主要农作物产量趋势分析与可视化
android·java·大数据·django·课程设计
企业数字化笔记13 小时前
固定资产历史数据怎么导入系统?Excel模板、字段映射和数据校验
android·java·数据库·后端
龙之叶13 小时前
Android出海系列-GMS认证介绍
android
潮族大Z13 小时前
Android性能优化:启动、内存、卡顿的一站式排查手册
android
事圆则缓15 小时前
从suspend字节码到 Retrofit 协程桥:挂起函数识别与恢复
android·retrofit
杉氧16 小时前
RN 性能调优指南:重渲染(Re-renders)控制与长列表(FlatList)优化
android·前端·react native
Coffeeee16 小时前
claude-video 一个让你的Agent拥有看视频能力的Skill
android·人工智能·aigc