下面列出了怎么用android.text.DynamicLayout的API类实例代码及写法,或者点击链接到github查看源代码。
public void makeDisplayObjects(float maxw,int just)
{
textPaint.setTextSize(textSize);
textPaint.setTypeface(typeFace);
textPaint.setColor(colour);
dynLayout = new DynamicLayout(textBuffer(),textPaint,(int)width, Layout.Alignment.ALIGN_NORMAL,1,0,true);
displayObjectsValid = true;
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Initialize the values;
initializeValues();
// Build a new Dynamic Layout with the available width since we can only provide width when the dynamic layout is created
mDynamicLayout = new DynamicLayout(mStringBuilder, mStringBuilder, mTextPaint, w, Layout.Alignment.ALIGN_CENTER, 1.0f, 1.0f, true);
}
public void draw(Canvas canvas) {
if (shouldDrawText()) {
float[] textPosition = getBestTextPosition();
if (!TextUtils.isEmpty(mTitle)) {
canvas.save();
if (hasRecalculated) {
mDynamicTitleLayout = new DynamicLayout(mTitle, titlePaint,
(int) textPosition[2], Layout.Alignment.ALIGN_NORMAL,
1.0f, 1.0f, true);
}
if (mDynamicTitleLayout != null) {
canvas.translate(textPosition[0], textPosition[1]);
mDynamicTitleLayout.draw(canvas);
canvas.restore();
}
}
if (!TextUtils.isEmpty(mDetails)) {
canvas.save();
if (hasRecalculated) {
mDynamicDetailLayout = new DynamicLayout(mDetails, textPaint,
(int) textPosition[2],
Layout.Alignment.ALIGN_NORMAL,
1.2f, 1.0f, true);
}
float offsetForTitle = mDynamicTitleLayout != null ? mDynamicTitleLayout.getHeight() :
0;
if (mDynamicDetailLayout != null) {
canvas.translate(textPosition[0], textPosition[1] + offsetForTitle);
mDynamicDetailLayout.draw(canvas);
canvas.restore();
}
}
}
hasRecalculated = false;
}
public void draw(Canvas canvas) {
if (shouldDrawText()) {
float[] textPosition = getBestTextPosition();
if (!TextUtils.isEmpty(mTitle)) {
canvas.save();
if (hasRecalculated) {
mDynamicTitleLayout = new DynamicLayout(mTitle, titlePaint,
(int) textPosition[2], Layout.Alignment.ALIGN_NORMAL,
1.0f, 1.0f, true);
}
if (mDynamicTitleLayout != null) {
canvas.translate(textPosition[0], textPosition[1]);
mDynamicTitleLayout.draw(canvas);
canvas.restore();
}
}
if (!TextUtils.isEmpty(mDetails)) {
canvas.save();
if (hasRecalculated) {
mDynamicDetailLayout = new DynamicLayout(mDetails, textPaint,
(int) textPosition[2],
Layout.Alignment.ALIGN_NORMAL,
1.2f, 1.0f, true);
}
float offsetForTitle = mDynamicTitleLayout != null ? mDynamicTitleLayout.getHeight() :
0;
if (mDynamicDetailLayout != null) {
canvas.translate(textPosition[0], textPosition[1] + offsetForTitle);
mDynamicDetailLayout.draw(canvas);
canvas.restore();
}
}
}
hasRecalculated = false;
}
public DynamicLayout layout()
{
if (dynLayout == null || !displayObjectsValid)
makeDisplayObjects(width,0);
return dynLayout;
}
public DynamicLayout layout()
{
return ((OBScrollingTextLayer)layer).layout();
}
/**
* 这里构建一个Layout,用于辅助计算,不是TextView关联的Layout
* @param text
* @return
*/
protected Layout buildCalculateLayout(CharSequence text){
TextPaint paint = copyPaint();
return new DynamicLayout(text,paint, mLayoutWidth,mHost.getLayout().getAlignment(),mHost.getLayout().getSpacingMultiplier(),mHost.getLayout().getSpacingAdd(),mHost.getIncludeFontPadding());
}