当前位置: 首页>>代码示例>>Java>>正文


Java Layout.getDesiredWidth方法代码示例

本文整理汇总了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);
    }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:25,代码来源:DanmakuItem.java

示例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();
    }
}
 
开发者ID:alibaba,项目名称:Virtualview-Android,代码行数:19,代码来源:TMReminderTagsView.java

示例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);
    }
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:25,代码来源:WebappUrlBar.java

示例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;
}
 
开发者ID:amap-demo,项目名称:weex-3d-map,代码行数:24,代码来源:WXTextDomObject.java

示例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;
}
 
开发者ID:weexext,项目名称:ucar-weex-core,代码行数:15,代码来源:WXTextDomObject.java

示例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);
}
 
开发者ID:xieyangxuejun,项目名称:CommentView,代码行数:41,代码来源:CanvasTextArea.java

示例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){

    }
}
 
开发者ID:fengdongfei,项目名称:CXJPadProject,代码行数:36,代码来源:EllipsizeTextView.java

示例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);
}
 
开发者ID:fengdongfei,项目名称:CXJPadProject,代码行数:32,代码来源:EllipsizeTextView.java

示例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);
}
 
开发者ID:lsjwzh,项目名称:FastTextView,代码行数:11,代码来源:SingleLineTextView.java

示例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;
}
 
开发者ID:MCMrARM,项目名称:revolution-irc,代码行数:12,代码来源:AlignToPointSpan.java

示例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);
        }
    }
}
 
开发者ID:alibaba,项目名称:LuaViewPlayground,代码行数:50,代码来源:TextUtil.java

示例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);
}
 
开发者ID:shazam,项目名称:reflow-animator,代码行数:7,代码来源:ReflowTextAnimatorHelper.java

示例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}
				)
		);
	}
}
 
开发者ID:lambdasoup,项目名称:blockvote,代码行数:55,代码来源:HistoryView.java


注:本文中的android.text.Layout.getDesiredWidth方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。