本文整理匯總了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();
}