【Android学习笔记】canvas drawText居中问题
2016-07-29 18:51
357 查看
在网上看很多说canvas的text的居中是以baseline为基准的,但是用了他们的代码,永远无法使text居中,要么可以居中,但是换一个分辨率,就又偏差了。
所以我是直接用height-(mFontMetricsInt.bottom)/2来设置。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getMeasuredHeight();
int width = getWidth();
paint.setTextSize(getHeight());
int textWidth = getTextWidth("完成" + progress + "%");
textTop = height - mFontMetricsInt.bottom / 2;
canvas.drawText("完成" + progress + "%", textLeft, textTop, paint);
}
这样设置canvas的textTop就大致适用于其他分辨率的手机了。
所以我是直接用height-(mFontMetricsInt.bottom)/2来设置。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int height = getMeasuredHeight();
int width = getWidth();
paint.setTextSize(getHeight());
int textWidth = getTextWidth("完成" + progress + "%");
textTop = height - mFontMetricsInt.bottom / 2;
canvas.drawText("完成" + progress + "%", textLeft, textTop, paint);
}
这样设置canvas的textTop就大致适用于其他分辨率的手机了。
相关文章推荐
- Android笔记 week3.1之"自定义控件"
- Android Studio 引用和生成aar
- 策略模式 - Android.TabHost
- Android Studio 识别不到 Genymotion
- Android之EditText控件
- Android自定义搜索框
- android -wifi热点设置问题
- Android中make命令
- NDK相关组件及作用
- android developer tiny share-20160729
- android developer tiny share-20160729
- Android 细碎知识点:
- Android Studio插件安装及使用Genymotion模拟器
- Android Beacon开发
- Android开发异常javabinder !!! FAILED BINDER TRANSACTION !!!
- RecyclerView技术栈
- 基于Android与.net服务器RSA加密算法的数据加密
- Android中ExpandableListView的使用
- android- 对话框中弹出edittext 不能调出输入法
- Android string-array简单使用