android中static方法,StaticLayout如何在Android中使用?
StaticLayout(similar to DynamicLayout and BoringLayout)用于在画布上布局和绘制文本.它通常用于以下任务:
>测量布局后多行文字的大小.
>在位图图像上绘制文本.
>创建一个处理自己的文本布局的自定义视图(而不是使用嵌入的TextView创建复合视图). TextView本身使用StaticLayout internally.
测量文字大小
单线
如果您只有一行文本,则可以使用Paint或TextPaint进行测量.
String text = "This is some text."
TextPaint myTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
mTextPaint.setColor(0xFF000000);
float width = mTextPaint.measureText(text);
float height = -mTextPaint.ascent() + mTextPaint.descent();
多行
但是,如果有换行并且您需要高度,那么最好使用StaticLayout.您提供宽度,然后您可以从StaticLayout获取高度.
String text = "This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.";
TextPaint myTextPaint = new TextPaint();
myTextPaint.setAntiAlias(true);
myTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
myTextPaint.setColor(0xFF000000);
int width = 200;
Layout.Alignment alignment = Layout.Alignment.ALIGN_NORMAL;
float spacingMultiplier = 1;
float spacingAddition = 0;
boolean includePadding = false;
StaticLayout myStaticLayout = new StaticLayout(text, myTextPaint, width, alignment, spacingMultiplier, spacingAddition, includePadding);
float height = myStaticLayout.getHeight();
新API
如果您想使用较新的StaticLayout.Builder(可从API 23获得),您可以获得如下布局:
StaticLayout.Builder builder = StaticLayout.Builder.obtain(text, 0, text.length(), myTextPaint, width);
StaticLayout myStaticLayout = builder.build();
您可以使用点表示法来添加额外设置:
StaticLayout.Builder builder = StaticLayout.Builder.obtain(text, 0, text.length(), myTextPaint, width)
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setLineSpacing(spacingMultiplier, spacingAddition)
.setIncludePad(includePadding)
.setMaxLines(5);
StaticLayout myStaticLayout = builder.build();
在图像上写文字
我可能会在将来扩展它,但是现在请参阅this post以获取使用StaticLayout并返回位图的方法的示例.
进行自定义文本处理视图
以下是使用StaticLayout的自定义视图的示例.它的行为类似于简单的TextView.当文本太长而无法放在屏幕上时,它会自动换行并增加其高度.
码
MyView.java
public class MyView extends View {
String mText = "This is some text.";
TextPaint mTextPaint;
StaticLayout mStaticLayout;
// use this constructor if creating MyView programmatically
public MyView(Context context) {
super(context);
initLabelView();
}
// this constructor is used when created from xml
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();
}
private void initLabelView() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
mTextPaint.setColor(0xFF000000);
// default to a single line of text
int width = (int) mTextPaint.measureText(mText);
mStaticLayout = new StaticLayout(mText, mTextPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
// New API alternate
//
// StaticLayout.Builder builder = StaticLayout.Builder.obtain(mText, 0, mText.length(), mTextPaint, width)
// .setAlignment(Layout.Alignment.ALIGN_NORMAL)
// .setLineSpacing(1, 0) // multiplier, add
// .setIncludePad(false);
// mStaticLayout = builder.build();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Tell the parent layout how big this view would like to be
// but still respect any requirements (measure specs) that are passed down.
// determine the width
int width;
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthRequirement = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
width = widthRequirement;
} else {
width = mStaticLayout.getWidth() + getPaddingLeft() + getPaddingRight();
if (widthMode == MeasureSpec.AT_MOST) {
if (width > widthRequirement) {
width = widthRequirement;
// too long for a single line so relayout as multiline
mStaticLayout = new StaticLayout(mText, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
}
}
}
// determine the height
int height;
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightRequirement = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode == MeasureSpec.EXACTLY) {
height = heightRequirement;
} else {
height = mStaticLayout.getHeight() + getPaddingTop() + getPaddingBottom();
if (heightMode == MeasureSpec.AT_MOST) {
height = Math.min(height, heightRequirement);
}
}
// Required call: set width and height
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// do as little as possible inside onDraw to improve performance
// draw the text on the canvas after adjusting for padding
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
mStaticLayout.draw(canvas);
canvas.restore();
}
}
activity_main.xml中
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_vertical_margin"
tools:context="com.example.layoutpractice.MainActivity">
android:layout_centerHorizontal="true"
android:background="@color/colorAccent"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
笔记
> This,this和this对于学习如何创建自定义文本处理视图很有用.
>如果要添加可以从代码或xml设置的自定义属性,请参阅Creating a View Class.
android中static方法,StaticLayout如何在Android中使用?相关推荐
- jquery中Live方法不可用,Jquery中Live方法失效
jquery中Live方法不可用,Jquery中Live方法失效 >>>>>>>>>>>>>>>>> ...
- android开发使用c+_如何在Android项目中开始使用C ++代码
android开发使用c+ by Onur Tuna 通过Onur Tuna 如何在Android项目中开始使用C ++代码 (How to start using C++ code in your ...
- android 工具栏沉浸 下拉,如何在Android应用中实现一个沉浸式状态栏效果
如何在Android应用中实现一个沉浸式状态栏效果 发布时间:2020-12-08 17:04:42 来源:亿速云 阅读:151 作者:Leah 这篇文章将为大家详细讲解有关如何在Android应用中 ...
- android 文本后图标_如何在Android中更改文本,图标等的大小
android 文本后图标 Let's face it: no matter how good the screens are on our phones and tablets, the text ...
- android studio点击图片,如何在Android Studio中的模拟器图库中添加图像?
如何在Android Studio中的模拟器图库中添加图像? 我正在开发图像过滤器应用程序. 但是,如果我没有任何图像,就无法真正尝试. 我知道我可以在电话中对其进行测试,但这并不相同,因为我需要错误 ...
- android 调出键盘表情_如何在Android的G板键盘中搜索表情符号和GIF | MOS86
老实说,这里是1010mh1112 GIF和表情符号是新形式的沟通.像他们似乎愚蠢的,他们以某种方式添加了一个额外的层次,我们与朋友和家人通过文本或即时消息交互的方式,否则可能会干燥.虽然表情符号长期 ...
- android开发 转跳功能,如何在Android中利用Intent实现一个页面跳转功能
如何在Android中利用Intent实现一个页面跳转功能 发布时间:2021-02-20 17:06:31 来源:亿速云 阅读:113 作者:Leah 本篇文章为大家展示了如何在Android中利用 ...
- android自定义控件是一个 内部类 如何在xml中引用,android 自定义view属性
android 自定义view属性 一个完美的自定义控件也可以添加xml来配置属性和风格.要实现这一点,可按照下列步骤来做: 1) 添加自定义属性到xml文件中 2) 在xml的中,指定属性的值 3) ...
- android中的后退功能,如何在Android应用中实现一个返回键功能
如何在Android应用中实现一个返回键功能 发布时间:2020-11-23 16:28:56 来源:亿速云 阅读:89 作者:Leah 今天就跟大家聊聊有关如何在Android应用中实现一个返回键功 ...
最新文章
- 空间点像素索引(三)
- 1.9 函数-C++编程模块
- 图解首次使用流光5.0
- tampermonkey怎么不能用了_发现键盘数字小键盘不能用怎么办?
- 4ask调制与解调的matlab_LTE中的调制和编码
- springmvc dao怎么可以不写实现类_SpringMVC(一)细聊ContextLoaderListener 是怎么被加载的...
- java opencv orb_opencv python ORB算法
- 不要误解【流程管理】这个概念
- echarts中国地图及经纬度json
- python如何屏幕截图_Python实现屏幕截图的两种方式
- 《冰河世纪》特效指导罗皓做客【ftrack聊天室】
- Android4种网络连接方式HttpClient、HttpURLConnection、OKHttp和Volley优缺点和性能对比
- 计算机一个远程控制用户怎么回事,电脑不能被远程控制怎么办
- 【windows版本】 db2数据库安装与使用
- 问题解决:from conda.cli import main ModuleNotFoundError: No module named 'conda'
- 你需要知道的Linux安全
- asp毕业设计——基于asp+access的房产信息管理系统设计与实现(毕业论文+程序源码)——房产信息管理系统
- 物联网(IOT)之常见物联网通信技术概览-无线篇③
- 陀螺稳定云台控制流程图
- CSAP_MAT_BOM_MAINTAIN修改问题
热门文章
- [ 浙江大学 程序设计专题 ] 四个专题代码 报告 PPT共享
- 去除警告,打电话,发信息,应用程序之间跳转,打印沙盒路径,字符串名字转换方法,包装导航控制器等的代码...
- 模拟namenode挂掉利用secondary namenode恢复
- linux下apache和jboss集群和负载均衡
- 程序员的个人发展注意事项
- npm安装过程出现的问题及解决方式
- [flask实践] 解决mysql数据库不支持中文的问题
- Python之Requests库的异常
- 关于C#中async/await中的异常处理(上)
- 解决vue中绝对定位或固定定位在底部的按钮随键盘移动的问题