android - Android中如何使用StaticLayout?

标签 android canvas text android-custom-view staticlayout

我需要构建自己的自定义 TextView,所以我一直在学习使用 StaticLayout 在 Canvas 上绘制文本。这比直接使用 Canvas.drawText() 更好,或者 documentation说。但是,该文档没有给出如何做到这一点的任何示例。对 StaticLayout.Builder 的引用仅含糊不清。做这件事的新方法。

我找到了一个例子 here但它似乎有点过时了。

我终于知道怎么做,所以我在下面添加我的解释。

最佳答案

StaticLayout (similar to DynamicLayout and BoringLayout) 用于在 Canvas 上布局和绘制文本。它通常用于以下任务:

  • 测量多行文本布局后的大小。
  • 在位图图像上绘制文本。
  • 制作一个处理其自己的文本布局的自定义 View (而不是使用嵌入的 TextView 制作一个复合 View )。 TextView 本身使用 StaticLayout internally .

测量文字大小

单行

如果你只有一行文字,你可以用 PaintTextPaint 来衡量它。

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(spacingAddition, spacingMultiplier)
        .setIncludePad(includePadding)
        .setMaxLines(5);
StaticLayout myStaticLayout = builder.build();

在图像上书写文字

我以后可能会进一步扩展它,但现在请参阅 this post有关使用 StaticLayout 并返回位图的方法示例。

制作自定义文本处理 View

这是一个使用 StaticLayout 的自定义 View 示例。它的行为就像一个简单的 TextView。当文本太长而无法在屏幕上显示时,它会自动换行并增加其高度。

enter image description here

代码

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(0, 1) // add, multiplier
        //        .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在学习如何制作自定义文本处理 View 时很有用。

  • Creating a View Class如果您想添加可以从代码或 xml 设置的自定义属性。

关于android - Android中如何使用StaticLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779934/

相关文章:

ANDROID使用非标准SIP端口

java - 在 Activity 类中使用哪个上下文?

javascript - 如何在 Kineticjs 中对曲线路径上的对象进行动画处理

python 魔杖 : how to get the text bolded?

android - 我可以将 View 作为 Android 上数据绑定(bind) BindingAdapter 的输入吗?

android - 是否有向 Eclipse 中的 list 添加 Activity 的快捷方式?

html - 在 HTML canvas 中绘制的文本有边缘

javascript - 存储用户稍后可以检索的 JS 变量和 Canvas 数据的替代方法?

python - 从 .txt 文件中获取行并复制到 word .doc

c++ - 使用 C++ 或任何 Windows 脚本语言格式化文本数据