
我需要构建自己的自定义TextVIEw,所以我一直在学习StaticLayout在画布上绘制文本.这比直接使用Canvas.drawText()更好,或者documentation说.但是,文档没有给出任何示例.只有一个含糊的提及StaticLayout.Builder是更新的方式.
我找到了一个例子here,但似乎有点过时了.
我终于工作了,但是我在下面添加了我的解释.
解决方法:
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中
<?xml version="1.0" enCoding="utf-8"?><relativeLayout 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"> <com.example.layoutpractice.MyVIEw androID:layout_centerHorizontal="true" androID:background="@color/colorAccent" androID:padding="10dp" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"/></relativeLayout>笔记
> This,this和this对于学习如何创建自定义文本处理视图很有用.
>如果要添加可以从代码或xml设置的自定义属性,请参阅Creating a View Class.
以上是内存溢出为你收集整理的StaticLayout如何在Android中使用?全部内容,希望文章能够帮你解决StaticLayout如何在Android中使用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)