本文整理匯總了Java中android.text.Layout.getDesiredWidth方法的典型用法代碼示例。如果您正苦於以下問題:Java Layout.getDesiredWidth方法的具體用法?Java Layout.getDesiredWidth怎麽用?Java Layout.getDesiredWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.text.Layout
的用法示例。
在下文中一共展示了Layout.getDesiredWidth方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: measure
import android.text.Layout; //導入方法依賴的package包/類
private void measure() {
TextPaint tp = new TextPaint();
tp.setAntiAlias(true);
tp.setColor(mTextColor);
tp.setTextSize(mTextSize);
strokePaint.setTextSize(mTextSize);
// tp.setShadowLayer(4, 0, 0, Color.BLACK);
mContentHeight = getFontHeight(tp);
staticLayout = new StaticLayout(mContent,
tp,
(int) Layout.getDesiredWidth(mContent, 0, mContent.length(), tp) + 1,
Layout.Alignment.ALIGN_NORMAL,
1.0f,
0.0f,
false);
mContentWidth = staticLayout.getWidth();
borderStaticLayout = new StaticLayout(mContent,
strokePaint,
(int) Layout.getDesiredWidth(mContent, 0, mContent.length(), tp) + 1,
Layout.Alignment.ALIGN_NORMAL,
1.0f,
0.0f,
false);
}
示例2: setTags
import android.text.Layout; //導入方法依賴的package包/類
public void setTags(String[] tags) {
if (tags != null && this.tags != tags) {
this.tags = tags;
tagsWidth = 0;
tagsHeight = (int) ((textFontMetrics.descent - textFontMetrics.ascent) + 0.5f);
this.eachTagWidth = new int[tags.length];
for (int i = 0; i < tags.length; i++) {
int tagWdith = (int) (Layout.getDesiredWidth(tags[i] != null ? tags[i] : "", textPaint) + 0.5f) + 2 * hPadding;
eachTagWidth[i] = tagWdith;
tagsWidth += tagWdith;
if (i < tags.length - 1) {
tagsWidth += tagsGap;
}
}
requestLayout();
invalidate();
}
}
示例3: onLayoutChange
import android.text.Layout; //導入方法依賴的package包/類
/**
* Show the end of the URL rather than the beginning.
*/
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
int oldTop, int oldRight, int oldBottom) {
Layout layout = mUrlBar.getLayout();
if (layout == null) return;
// Android doesn't account for the compound Drawable in its width calculations, leading to
// improper scrolling and even Android improperly placing the horizontal fade in its
// TextView calculation. Get around it by calculating that width manually: crbug.com/303908
int urlBarWidth = mUrlBar.getWidth();
int iconWidth =
mCurrentIconResource == 0 ? 0 : mIconResourceWidths.get(mCurrentIconResource);
int availableTextWidth = urlBarWidth - iconWidth;
int desiredWidth = (int) Layout.getDesiredWidth(layout.getText(), layout.getPaint());
if (desiredWidth > availableTextWidth) {
mUrlBar.scrollTo(desiredWidth - availableTextWidth, 0);
} else {
mUrlBar.scrollTo(0, 0);
}
}
示例4: getTextWidth
import android.text.Layout; //導入方法依賴的package包/類
/**
* Get text width according to constrain of outerWidth with and forceToDesired
* @param textPaint paint used to measure text
* @param outerWidth the width that css-layout desired.
* @param forceToDesired if set true, the return value will be outerWidth, no matter what the width
* of text is.
* @return if forceToDesired is false, it will be the minimum value of the width of text and
* outerWidth in case of outerWidth is defined, in other case, it will be outer width.
*/
private float getTextWidth(TextPaint textPaint,float outerWidth, boolean forceToDesired) {
float textWidth;
if (forceToDesired) {
textWidth = outerWidth;
} else {
float desiredWidth = Layout.getDesiredWidth(spanned, textPaint);
if (CSSConstants.isUndefined(outerWidth) || desiredWidth < outerWidth) {
textWidth = desiredWidth;
} else {
textWidth = outerWidth;
}
}
return textWidth;
}
示例5: if
import android.text.Layout; //導入方法依賴的package包/類
/** package **/ float getTextWidth(TextPaint textPaint,float outerWidth, boolean forceToDesired) {
float textWidth;
if (forceToDesired) {
textWidth = outerWidth;
} else {
float desiredWidth = Layout.getDesiredWidth(spanned, textPaint);
if (CSSConstants.isUndefined(outerWidth) || desiredWidth < outerWidth) {
textWidth = desiredWidth;
} else {
textWidth = outerWidth;
}
}
return textWidth;
}
示例6: onMeasure
import android.text.Layout; //導入方法依賴的package包/類
protected void onMeasure(int i, int i2) {
String generateCacheKey = generateCacheKey();
this.mCacheText = (TextCache) getAreaCache(generateCacheKey);
if (this.mCacheText == null || this.mCacheText.mLayout == null || this.mMeasureDirty) {
this.mCacheText = new TextCache();
addAreaCache(generateCacheKey, this.mCacheText);
int mode = View.MeasureSpec.getMode(i);
int mode2 = View.MeasureSpec.getMode(i2);
int size = View.MeasureSpec.getSize(i);
int size2 = View.MeasureSpec.getSize(i2);
if (mode == 1073741824) {
this.mCacheText.measuredWidth = size;
} else {
this.mBoring = BoringLayout.isBoring(this.mText, this.mPaint, UNKNOWN_BORING);
if (this.mBoring == null || this.mBoring == UNKNOWN_BORING) {
this.mCacheText.measuredWidth = (int) Layout.getDesiredWidth(this.mText, this.mPaint);
} else {
this.mCacheText.measuredWidth = this.mBoring.width;
}
TextCache textCache = this.mCacheText;
textCache.measuredWidth = textCache.measuredWidth + (this.paddingLeft + this.paddingRight);
if (mode == Integer.MIN_VALUE) {
this.mCacheText.measuredWidth = Math.min(size, this.mCacheText.measuredWidth);
}
}
mode = (this.mCacheText.measuredWidth - this.paddingLeft) - this.paddingRight;
this.mCacheText.mLayout = makeNewLayout(mode, this.mBoring, mode, false);
if (mode2 == 1073741824) {
this.mCacheText.measuredHeight = size2;
} else {
this.mCacheText.measuredHeight = getDesiredHeight();
if (mode2 == Integer.MIN_VALUE) {
this.mCacheText.measuredHeight = Math.min(size2, this.mCacheText.measuredHeight);
}
}
setMeasuredDimension(this.mCacheText.measuredWidth, this.mCacheText.measuredHeight);
return;
}
setMeasuredDimension(this.mCacheText.measuredWidth, this.mCacheText.measuredHeight);
}
示例7: adjustEllipsizeEndText
import android.text.Layout; //導入方法依賴的package包/類
private void adjustEllipsizeEndText(Layout layout) {
try {
final CharSequence originText = getText();
if(TextUtils.isEmpty(originText)){
return;
}
final CharSequence restSuffixText = originText.subSequence(
originText.length() - mEllipsizeIndex, originText.length());
final int width = layout.getWidth() - getPaddingLeft() - getPaddingRight();
final int maxLineCount = computeMaxLineCount(layout);
final int lastLineWidth = (int) layout.getLineWidth(maxLineCount - 1);
final int mLastCharacterIndex = layout.getLineEnd(maxLineCount - 1);
final int suffixWidth = (int) (Layout.getDesiredWidth(mEllipsizeText, getPaint()) +
Layout.getDesiredWidth(restSuffixText, getPaint())) + 1;
if (lastLineWidth + suffixWidth > width) {
final int widthDiff = lastLineWidth + suffixWidth - width;
final int removedCharacterCount = computeRemovedEllipsizeEndCharacterCount(widthDiff,
originText.subSequence(0, mLastCharacterIndex));
setText(originText.subSequence(0, mLastCharacterIndex - removedCharacterCount));
append(mEllipsizeText);
append(restSuffixText);
} else {
setText(originText.subSequence(0, mLastCharacterIndex));
append(mEllipsizeText);
append(restSuffixText);
}
}catch (IndexOutOfBoundsException e){
}
}
示例8: computeRemovedEllipsizeEndCharacterCount
import android.text.Layout; //導入方法依賴的package包/類
private int computeRemovedEllipsizeEndCharacterCount(final int widthDiff, final CharSequence text) {
if (TextUtils.isEmpty(text)) {
return 0;
}
final List<Range<Integer>> characterStyleRanges = computeCharacterStyleRanges(text);
final String textStr = text.toString();
// prevent the subString from containing messy code when the given string contains emotion
int characterIndex = text.length();
int codePointIndex = textStr.codePointCount(0, text.length());
int currentRemovedWidth = 0;
while (codePointIndex > 0 && widthDiff > currentRemovedWidth) {
codePointIndex--;
characterIndex = textStr.offsetByCodePoints(0, codePointIndex);
// prevent the subString from containing messy code when the given string contains CharacterStyle
Range<Integer> characterStyleRange = computeCharacterStyleRange(characterStyleRanges, characterIndex);
if (characterStyleRange != null) {
characterIndex = characterStyleRange.getLower();
codePointIndex = textStr.codePointCount(0, characterIndex);
}
currentRemovedWidth = (int) Layout.getDesiredWidth(
text.subSequence(characterIndex, text.length()),
getPaint());
}
return text.length() - textStr.offsetByCodePoints(0, codePointIndex);
}
示例9: onMeasure
import android.text.Layout; //導入方法依賴的package包/類
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mLayout == null && !TextUtils.isEmpty(mText)) {
BoringLayout.Metrics fm = new BoringLayout.Metrics();
fm.width = (int) Layout.getDesiredWidth(mText, mTextPaint);
mLayout = BoringLayout.make(mText, mTextPaint, MeasureSpec.getSize(widthMeasureSpec),
TextViewAttrsHelper.getLayoutAlignment(this, getGravity()), mAttrsHelper.mSpacingMultiplier, mAttrsHelper.mSpacingAdd, fm, true);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
示例10: apply
import android.text.Layout; //導入方法依賴的package包/類
public static CharSequence apply(TextView textView, CharSequence text) {
if (text == null || !(text instanceof Spannable))
return text;
Spannable s = (Spannable) text;
for (AlignToPointSpan span : s.getSpans(0, text.length(),
AlignToPointSpan.class)) {
span.mMargin = (int) Layout.getDesiredWidth(text, 0, s.getSpanStart(span.mAnchor),
textView.getPaint());
}
return text;
}
示例11: adjustSize
import android.text.Layout; //導入方法依賴的package包/類
/**
* 改變外框大小,適應文字
* 如果當前文字隻有一行,則縮到外框適應文字大小
* 如果文字有多行,則寬度不變,縮小高度到適應文字大小
* 文字外框的位置不變
*
* @param view
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void adjustSize(TextView view) {
if (view != null && view.getLayoutParams() != null) {
/*
//更好的實現方式,但是ios那邊不支持這種方式
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(layoutParams);*/
// 廢棄的實現方式,使用WRAP_CONTENT最簡單,且這種方式算出來的width有bug
// @Deprecated
/*int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
int width, height;
if (lineCount > 1) {//多行,寬度不變,改變高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改變寬度&高度
final Paint paint = view.getPaint();
width = (int) paint.measureText(view.getText().toString()) + view.getPaddingLeft() + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}*/
int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
float width, height;
if (lineCount > 1) {//多行,寬度不變,改變高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改變寬度&高度
width = view.getPaddingLeft() + Layout.getDesiredWidth(view.getText(), view.getPaint()) + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}
if (view.getLayoutParams() != null) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (int) width;
layoutParams.height = (int) height;
view.setLayoutParams(layoutParams);
}
}
}
示例12: getSectionWidth
import android.text.Layout; //導入方法依賴的package包/類
private static int getSectionWidth(@Nonnull Layout layout, int sectionStart, int sectionEnd) {
CharSequence text = layout.getText();
TextPaint paint = layout.getPaint();
return (int) Layout.getDesiredWidth(text, sectionStart, sectionEnd, paint);
}
示例13: HistoryView
import android.text.Layout; //導入方法依賴的package包/類
public HistoryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
padding = getResources().getDimensionPixelSize(R.dimen.history_padding);
labelHeight = getResources().getDimensionPixelSize(R.dimen.history_label);
strokeWidth = getResources().getDimensionPixelSize(R.dimen.history_strokewidth);
sw1dPaint = makePaint(Option.SW, Interval.D1, strokeWidth);
sw7dPaint = makePaint(Option.SW, Interval.D7, strokeWidth);
sw30dPaint = makePaint(Option.SW, Interval.D30, strokeWidth);
bu1dPaint = makePaint(Option.BU, Interval.D1, strokeWidth);
bu7dPaint = makePaint(Option.BU, Interval.D7, strokeWidth);
bu30dPaint = makePaint(Option.BU, Interval.D30, strokeWidth);
// axes
axisPaint = new Paint();
axisPaint.setStrokeWidth(3.0f);
axisPaint.setColor(Color.DKGRAY);
axisPaint.setStyle(Paint.Style.STROKE);
axisPath = new Path();
// marks
marksPaint = new Paint();
marksPaint.setStrokeWidth(3.0f);
marksPaint.setColor(Color.LTGRAY);
marksPaint.setStyle(Paint.Style.STROKE);
marksPath = new Path();
// labels
yLabelTextPaint = new TextPaint();
yLabelTextPaint.setColor(Color.GRAY);
yLabelTextPaint.setTextSize(labelHeight);
yLabelTextPaint.setAntiAlias(true);
yLabelTextPaint.setTextAlign(Paint.Align.RIGHT);
xLabelTextPaint = new TextPaint();
xLabelTextPaint.setColor(Color.GRAY);
xLabelTextPaint.setTextSize(labelHeight);
xLabelTextPaint.setAntiAlias(true);
xLabelTextPaint.setTextAlign(Paint.Align.CENTER);
labelWidth = Layout.getDesiredWidth("100%", yLabelTextPaint);
if (isInEditMode()) {
setData(new MainViewModel.Data(
new Date(), new Date(), new float[]{0.1f, 0.2f, 0.15f, 0.18f},
new float[]{0.3f, 0.5f, 0.1f, 0.5f},
new float[]{0.3f, 0.5f, 0.1f, 0.5f},
new float[]{0.3f, 0.5f, 0.1f, 0.5f},
new float[]{0.3f, 0.5f, 0.1f, 0.5f},
new float[]{0.3f, 0.5f, 0.1f, 0.5f}
)
);
}
}