当前位置:网站首页>Detailed explanation of the use of staticlayout
Detailed explanation of the use of staticlayout
2022-07-05 10:11:00 【A bird carved in the desert】
stay Android In development ,Canvas.drawText Don't wrap , Even a very long string will only display one line , The excess will be hidden from the screen .
StaticLayout yes android A tool class for processing text in ,StaticLayout Solved the problem of word wrapping , As an example :
package com.zhy.someuitest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
public class StaticLayoutView extends View {
StaticLayout staticLayout;
public StaticLayoutView(Context context) {
this(context, null);
}
public StaticLayoutView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public StaticLayoutView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextSize(50);
String text = " stay Android In development ,Canvas.drawText Don't wrap , Even a very long string will only display one line , The excess will be hidden from the screen .StaticLayout yes android A tool class for processing text in ,StaticLayout Solved the problem of word wrapping ";
staticLayout = new StaticLayout(text, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
staticLayout.draw(canvas);
}
}

Staticlayout There are three construction parameters in :
With public StaticLayout(CharSequence source, int bufstart, int bufend,
TextPaint paint, int outerwidth,
Alignment align,
float spacingmult, float spacingadd,
boolean includepad,
TextUtils.TruncateAt ellipsize, int ellipsizedWidth) {
this(source, bufstart, bufend, paint, outerwidth, align,
TextDirectionHeuristics.FIRSTSTRONG_LTR,
spacingmult, spacingadd, includepad, ellipsize, ellipsizedWidth, Integer.MAX_VALUE);
} Explain the function of parameters
CharSequence source Need a string of branches
int bufstart The string that needs to be branched starts at the position of
int bufend Where does the string for the branch end
TextPaint paint Brush object
int outerwidth layout Width , Wrap when exceeded
Alignment align layout The right way to do it , Yes ALIGN_CENTER, ALIGN_NORMAL, ALIGN_OPPOSITE Three
float spacingmult Relative row spacing , Relative font size ,1.5f Indicates that the line spacing is 1.5 Times the font height .
float spacingadd How much to add to the base line spacing
boolean includepad,
TextUtils.TruncateAt ellipsize Where to start omitting
int ellipsizedWidth More than how many begin to omit
Add :TextView In fact, it is also used StaticLayout
边栏推荐
- Zblogphp breadcrumb navigation code
- RMS to EAP is simply implemented through mqtt
- 一个程序员的职业生涯到底该怎么规划?
- .Net之延迟队列
- 面试:List 如何根据对象的属性去重?
- Swift uses userdefaults and codable to save an array of class objects or structure instances
- Cross process communication Aidl
- 驱动制造业产业升级新思路的领域知识网络,什么来头?
- Tianlong Babu TLBB series - about items dropped from packages
- Windows uses commands to run kotlin
猜你喜欢

Observation cloud and tdengine have reached in-depth cooperation to optimize the cloud experience of enterprises

Energy momentum: how to achieve carbon neutralization in the power industry?

The comparison of every() and some() in JS uses a power storage plan

cent7安装Oracle数据库报错

ArcGIS Pro creating features

QT event filter simple case

Swift set pickerview to white on black background

TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries

硬核,你见过机器人玩“密室逃脱”吗?(附代码)

Advanced opencv:bgr pixel intensity map
随机推荐
Swift uses userdefaults and codable to save an array of class objects or structure instances
ArcGIS Pro 创建要素
Data visualization platform based on template configuration
Kotlin compose multiple item scrolling
自动化规范检查软件如何发展而来?
MySQL字符类型学习笔记
【小技巧】获取matlab中cdfplot函数的x轴,y轴的数值
程序员搞开源,读什么书最合适?
Click the picture in the mobile browser and the picture will not pop up
美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
《微信小程序-基础篇》小程序中的事件与冒泡
Coffeescript Chinese character to pinyin code
面试:Bitmap像素内存分配在堆内存还是在native中
双容水箱液位模糊PID控制系统设计与仿真(Matlab/Simulink)
Getting started with Apache dolphin scheduler (one article is enough)
Flutter development: a way to solve the problem of blank space on the top of listview
H. 265 introduction to coding principles
Meitu lost 300 million yuan in currency speculation for half a year. Huawei was exposed to expand its enrollment in Russia. Alphago's peers have made another breakthrough in chess. Today, more big new
[C language] the use of dynamic memory development "malloc"
橫向滾動的RecycleView一屏顯示五個半,低於五個平均分布