本文整理汇总了Java中android.text.StaticLayout.getDesiredWidth方法的典型用法代码示例。如果您正苦于以下问题:Java StaticLayout.getDesiredWidth方法的具体用法?Java StaticLayout.getDesiredWidth怎么用?Java StaticLayout.getDesiredWidth使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.StaticLayout
的用法示例。
在下文中一共展示了StaticLayout.getDesiredWidth方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: drawUnitText
import android.text.StaticLayout; //导入方法依赖的package包/类
private void drawUnitText(Canvas c) {
if (null != mUnitText && !TextUtils.isEmpty(mUnitText)) {
mUnitTextPaint.setColor(mUnitColor);
mUnitTextPaint.setTextSize(mUnitSize);
float startX = getWidth() - getPaddingRight() - StaticLayout.getDesiredWidth(mUnitText, 0, mUnitText.length(), mUnitTextPaint);
Paint.FontMetrics fontMetrics = mUnitTextPaint.getFontMetrics();
float startY = getVerticalSpace() / 2 + mUnitSize / 2 - fontMetrics.descent / 2;
c.drawText(mUnitText, startX, startY, mUnitTextPaint);
}
}
示例2: drawChild
import android.text.StaticLayout; //导入方法依赖的package包/类
@Override
public boolean drawChild(Canvas canvas, View child, long drawingTime) {
// rotateX
int centerY = getVerticalSpace() / 2;
int childCenterY = child.getTop() + child.getHeight() / 2;
float factor = (centerY - childCenterY) * 1f / centerY;
float alphaFactor = 1 - 0.7f * Math.abs(factor);
child.setAlpha(alphaFactor * alphaFactor * alphaFactor);
float scaleFactor = 1 - 0.3f * Math.abs(factor);
child.setScaleX(scaleFactor);
child.setScaleY(scaleFactor);
float rotateRadius = 2.0f * centerY / (float) Math.PI;
float rad = (centerY - childCenterY) * 1f / rotateRadius;
float offsetZ = rotateRadius * (1 - (float) Math.cos(rad));
float rotateDeg = rad * 180 / (float) Math.PI;
ViewCompat.setZ(child, -offsetZ);
child.setRotationX(rotateDeg);
float offsetY = centerY - childCenterY - rotateRadius * (float) Math.sin(rad) * 1.3f;
child.setTranslationY(offsetY);
// resize the text size if text can not be shown completely
if (child instanceof TextView) {
String data = ((TextView) child).getText().toString();
if (((TextView) child).getTextSize() == mTextSize) {
float finalTextSize = mTextSize;
float dataStringW = StaticLayout.getDesiredWidth(data, 0, data.length(), ((TextView) child).getPaint());
if (getHorizontalSpace() > 0 && dataStringW * 1.1f > getHorizontalSpace()) {
finalTextSize = getHorizontalSpace() / dataStringW / 1.1f * mTextSize;
}
((TextView) child).setTextSize(TypedValue.COMPLEX_UNIT_PX, finalTextSize);
}
}
return super.drawChild(canvas, child, drawingTime);
// // parent centerY ,item centerY
// int centerY = (getHeight() - getPaddingTop() - getPaddingBottom()) / 2;
// int childCenterY = child.getTop() + child.getHeight() / 2;
// // alpha
// float factor = (centerY - childCenterY) * 1f / centerY;
// float currentFactor = 1 - 0.7f * Math.abs(factor);
// child.setAlpha(currentFactor * currentFactor * currentFactor);
//
// // rotate radius
// float rotateRadius = 2.5f * centerY / (float) Math.PI;
// // deg
// float rad = (centerY - childCenterY) * 1f / rotateRadius;
// float rotateDeg = rad * 180 / (float) Math.PI;
// // for camera
// float offsetZ = rotateRadius * (1 - (float) Math.cos(rad));
// canvas.save();
// // offset Y for item rotate
// float offsetY = centerY - childCenterY - rotateRadius * (float) Math.sin(rad);
// canvas.translate(0, offsetY);
// mCamera.save();
// mCamera.translate(0, 0, offsetZ);
// mCamera.rotateX(rotateDeg);
// mCamera.getMatrix(mMatrix);
// mCamera.restore();
// mMatrix.preTranslate(-child.getWidth() / 2, -childCenterY);
// mMatrix.postTranslate(child.getWidth() / 2, childCenterY);
// canvas.concat(mMatrix);
// super.drawChild(canvas, child, drawingTime);
// canvas.restore();
// return true;
}
示例3: drawScaledText
import android.text.StaticLayout; //导入方法依赖的package包/类
private void drawScaledText(final String line, final Canvas canvas, final float y, final float desiredLineWidth, final TextPaint paint) {
String[] words = line.split(" ");
float lineWidthWithoutSpaces = StaticLayout.getDesiredWidth(line.replace(" ", ""), getPaint());
float spacing = (desiredLineWidth - lineWidthWithoutSpaces) / (words.length - 1);
float x = 0;
for (String word : words) {
float cw = StaticLayout.getDesiredWidth(word, paint);
canvas.drawText(word, x, y, paint);
x += cw + spacing;
}
}
示例4: drawTextWithJustify
import android.text.StaticLayout; //导入方法依赖的package包/类
/**
* 重绘文字,两端对齐
*
* @param canvas
*/
private void drawTextWithJustify(Canvas canvas) {
// 文字画笔
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
String text_str = getText().toString();
// 当前所在行的Y向偏移
int currentLineOffsetY = getPaddingTop();
currentLineOffsetY += getTextSize();
Layout layout = getLayout();
//循环每一行,绘制文字
for (int i = 0; i < layout.getLineCount(); i++) {
int lineStart = layout.getLineStart(i);
int lineEnd = layout.getLineEnd(i);
//获取到TextView每行中的内容
String line_str = text_str.substring(lineStart, lineEnd);
// 获取每行字符串的宽度(不包括字符间距)
float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());
if (isLineNeedJustify(line_str)) {
//最后一行不需要重绘
if (i == layout.getLineCount() - 1) {
canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
} else {
drawJustifyTextForLine(canvas, line_str, desiredWidth, currentLineOffsetY);
}
} else {
canvas.drawText(line_str, getPaddingLeft(), currentLineOffsetY, textPaint);
}
//更新行Y向偏移
currentLineOffsetY += getLineHeight();
}
}
示例5: calculatorCharPositionToLeft
import android.text.StaticLayout; //导入方法依赖的package包/类
/**
* 计算字符距离控件左侧的位移
*
* @param line 字符所在行
* @param charOffset 字符偏移量
*/
private float calculatorCharPositionToLeft(int line, int charOffset) {
String text_str = getText().toString();
Layout layout = getLayout();
int lineStart = layout.getLineStart(line);
int lineEnd = layout.getLineEnd(line);
String line_str = text_str.substring(lineStart, lineEnd);
if (line_str.equals("\n"))
return getPaddingLeft();
// 最左侧
if (lineStart == charOffset)
return getPaddingLeft();
// 最右侧
if (charOffset == lineEnd - 1)
return mViewTextWidth + getPaddingLeft();
float desiredWidth = StaticLayout.getDesiredWidth(text_str, lineStart, lineEnd, getPaint());
// 中间位置
// 计算相邻字符之间需要填充的宽度
// (TextView内容的实际宽度 - 该行字符串的宽度)/(字符个数-1)
float insert_blank = (mViewTextWidth - desiredWidth) / (line_str.length() - 1);
// 计算当前字符左侧所有字符的宽度
float allLeftCharWidth = StaticLayout.getDesiredWidth(text_str.substring(lineStart, charOffset), getPaint());
// 相邻字符之间需要填充的宽度 + 当前字符左侧所有字符的宽度
return insert_blank * (charOffset - lineStart) + allLeftCharWidth + getPaddingLeft();
}