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();
相关推荐
apihz9 小时前
全球域名 WHOIS 信息实时查询免费 API 接口教程,支持1000+后缀
android·网络·网络协议·tcp/ip·apache·域名·whois
非典型Android程序员11 小时前
Binder机制学习总结
android
AI2中文网11 小时前
App Inventor 2 音频播放器 vs 音效 vs 视频播放器:三个媒体组件,选错了App直接卡死
android·音视频·媒体
李剑一11 小时前
uni-app x 实现本地 JSON 文件的导入导出(APP端可用)
android·前端·uni-app
喜欢打篮球的普通人11 小时前
LLVM后端指令选择
android·java·javascript
hunterandroid11 小时前
Room 数据库迁移:让本地数据升级更稳
android·前端
阿巴斯甜11 小时前
Compose NavHost的使用:
android
雁鸣零落12 小时前
浏览器扩展 CaptionGo,在网页视频上显示双语字幕,支持 PC 和手机端使用
android·chrome·edge·firefox·安卓
2501_9151063212 小时前
iOS 软件测试工具性能监控、日志分析 KeyMob、Instruments等
android·ios·小程序·https·uni-app·iphone·webview
__Witheart__12 小时前
lunch选择的版型与版型对应的配置文件
android·linux