android 自定义View之将字体绘制在Rect中间
来源:互联网 发布:机床的重要性 知乎 编辑:程序博客网 时间:2024/06/05 22:57
@SuppressLint("ViewConstructor")
public class NTextView extends View { private String TAG="NtextView"; public NTextView(Context context) { super(context); } public NTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public NTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public void onDraw (Canvas canvas) { Rect targetRect = new Rect(50, 50, 1000, 200); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth(3); paint.setTextSize(80); String testString = "测试:ijkJQKA:1234"; paint.setColor(Color.CYAN); canvas.drawRect(targetRect, paint); paint.setColor(Color.RED); Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt(); int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics.top) / 2; int baseline2=targetRect.bottom-((targetRect.bottom-targetRect.top-fontMetrics.bottom+fontMetrics.top)/2+fontMetrics.bottom); Log.i(TAG, "onDraw: "+baseline2); Log.i(TAG, "onDraw: "+baseline); Log.i(TAG, "onDraw: bottom"+targetRect.bottom+" top"+targetRect.top +" fBottom"+fontMetrics.bottom+" ftop"+fontMetrics.top); // 下面这行是实现水平居中,drawText对应改为传入targetRect.centerX() paint.setTextAlign(Paint.Align.CENTER); canvas.drawText(testString, targetRect.centerX(), baseline, paint); }}
阅读全文
0 0
- android 自定义View之将字体绘制在Rect中间
- Android自定义控件之——文字圆形边框(将文字绘制在圆中间)
- Android自定义控件之——文字圆形边框(将文字绘制在圆中间)
- Android 自定义View之View的绘制
- Android自定义View之绘制、测量
- android 自定义view之文本绘制解析
- Android自定义view之- BitMap的绘制
- Android 自定义View学习之文字绘制
- android 自定义view之绘制(二)
- Android自定义View之绘制基础
- Android自定义View之View的绘制流程
- 自定义View绘制图像与移动字体
- Android 自定义View之自定义绘制(Custom Drawing)
- Android 自定义View(一) Paint、Rect、Canvas介绍
- android之View绘制
- 自定义View之绘制圆环
- 自定义View之绘制钟表
- Android自定义View --- 绘制圆环
- CF 839A
- 在Jekyll博客添加评论系统:gitment篇
- IoC容器
- 地牢逃脱 (BFS)
- vue2.0
- android 自定义View之将字体绘制在Rect中间
- LeetCode: 65.Valid Number
- HDU 1159 Common Subsequence(最长公共子序列(LCS) 动态规划(DP))
- 医院刚刚严重发声,情况紧急!全民必看!
- binary-tree-postorder-traversal
- C#中使用seriport和单片机进行串口长数据收发不全,解决办法
- Python自定义模块并发布与安装
- SQL—授权grant与回收revoke
- 字典树(trie树)