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


Java StaticLayout.getHeight方法代码示例

本文整理汇总了Java中android.text.StaticLayout.getHeight方法的典型用法代码示例。如果您正苦于以下问题:Java StaticLayout.getHeight方法的具体用法?Java StaticLayout.getHeight怎么用?Java StaticLayout.getHeight使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.text.StaticLayout的用法示例。


在下文中一共展示了StaticLayout.getHeight方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getTextHeightPixels

import android.text.StaticLayout; //导入方法依赖的package包/类
/**
 * Sets the text size of a clone of the view's {@link TextPaint} object
 * and uses a {@link StaticLayout} instance to measure the height of the text.
 *
 * @param source
 * @param availableWidthPixels
 * @param textSizePixels
 * @return the height of the text when placed in a view
 * with the specified width
 * and when the text has the specified size.
 */
private int getTextHeightPixels(
        CharSequence source,
        int availableWidthPixels,
        float textSizePixels) {
    // Make a copy of the original TextPaint object
    // since the object gets modified while measuring
    // (see also the docs for TextView.getPaint()
    // which states to access it read-only)
    TextPaint textPaintCopy = new TextPaint(getPaint());
    textPaintCopy.setTextSize(textSizePixels);
    // Measure using a StaticLayout instance
    StaticLayout staticLayout = new StaticLayout(
            source,
            textPaintCopy,
            availableWidthPixels,
            Layout.Alignment.ALIGN_NORMAL,
            mLineSpacingMultiplier,
            mLineSpacingExtra,
            true);
    return staticLayout.getHeight();
}
 
开发者ID:sega4revenge,项目名称:Sega,代码行数:33,代码来源:AutoResizeTextView.java

示例2: setText

import android.text.StaticLayout; //导入方法依赖的package包/类
public void setText(String text) {
    if (text == null || text.length() == 0) {
        setVisibility(GONE);
        return;
    }
    if (text != null && oldText != null && text.equals(oldText)) {
        return;
    }
    oldText = text;
    setVisibility(VISIBLE);
    if (AndroidUtilities.isTablet()) {
        width = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
    } else {
        width = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
    }
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    String help = LocaleController.getString("BotInfoTitle", R.string.BotInfoTitle);
    stringBuilder.append(help);
    stringBuilder.append("\n\n");
    stringBuilder.append(text);
    MessageObject.addLinks(stringBuilder);
    stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, help.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Emoji.replaceEmoji(stringBuilder, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
    try {
        textLayout = new StaticLayout(stringBuilder, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        width = 0;
        height = textLayout.getHeight() + AndroidUtilities.dp(4 + 18);
        int count = textLayout.getLineCount();
        for (int a = 0; a < count; a++) {
            width = (int) Math.ceil(Math.max(width, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
        }
    } catch (Exception e) {
        FileLog.e("tmessage", e);
    }
    width += AndroidUtilities.dp(4 + 18);
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:37,代码来源:BotHelpCell.java

示例3: getTextHeight

import android.text.StaticLayout; //导入方法依赖的package包/类
private int getTextHeight(CharSequence source, TextPaint paint, int width, float textSize) {
    // modified: make a copy of the original TextPaint object for measuring
    // (apparently the object gets modified while measuring, see also the
    // docs for TextView.getPaint() (which states to access it read-only)
    TextPaint paintCopy = new TextPaint(paint);
    // Update the text paint object
    paintCopy.setTextSize(textSize);
    // Measure using a static layout
    StaticLayout layout = new StaticLayout(source, paintCopy, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true);
    return layout.getHeight();
}
 
开发者ID:kingblaubart,项目名称:quidditchtimekeeper,代码行数:12,代码来源:AutoResizeTextView.java

示例4: getHeightS

import android.text.StaticLayout; //导入方法依赖的package包/类
private int getHeightS() {
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(getTextSize());
    StaticLayout staticLayout = new StaticLayout(textList.get(currentPosition), textPaint, getWidth() - getPaddingLeft() - getPaddingRight(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
    int height = staticLayout.getHeight();
    if (staticLayout.getLineCount() > getMaxLines()) {
        int lineCount = staticLayout.getLineCount();
        height = staticLayout.getLineBottom(getMaxLines() - 1);
    }
    return height;
}
 
开发者ID:huashengzzz,项目名称:SmartChart,代码行数:12,代码来源:BannerTextView.java

示例5: calculateHeight

import android.text.StaticLayout; //导入方法依赖的package包/类
public float calculateHeight() {
    if (text == null || ("" + text).trim().isEmpty()) {
        return 0;
    } else {
        final StaticLayout sl = new StaticLayout(text, paint, (int) (maxXRight - minXLeft), alignment, 1, 1, false);
        return sl.getHeight();
    }
}
 
开发者ID:florent37,项目名称:MyLittleCanvas,代码行数:9,代码来源:TextShape.java

示例6: onTestSize

import android.text.StaticLayout; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onTestSize(int suggestedSize, RectF availableSPace) {
    mPaint.setTextSize(suggestedSize);
    String text = getText().toString();
    boolean singleline = getMaxLines() == 1;
    if (singleline) {
        mTextRect.bottom = mPaint.getFontSpacing();
        mTextRect.right = mPaint.measureText(text);
    } else {
        StaticLayout layout = new StaticLayout(text, mPaint,
                mWidthLimit, Alignment.ALIGN_NORMAL, mSpacingMult,
                mSpacingAdd, true);
        // return early if we have more lines
        if (getMaxLines() != NO_LINE_LIMIT
                && layout.getLineCount() > getMaxLines()) {
            return 1;
        }
        mTextRect.bottom = layout.getHeight();
        int maxWidth = -1;
        for (int i = 0; i < layout.getLineCount(); i++) {
            if (maxWidth < layout.getLineWidth(i)) {
                maxWidth = (int) layout.getLineWidth(i);
            }
        }
        mTextRect.right = maxWidth;
    }

    mTextRect.offsetTo(0, 0);
    if (availableSPace.contains(mTextRect)) {
        // may be too small, don't worry we will find the best match
        return -1;
    } else {
        // too big
        return 1;
    }
}
 
开发者ID:eyeRS,项目名称:eyeRS,代码行数:38,代码来源:AutoResizeTextView.java

示例7: setText

import android.text.StaticLayout; //导入方法依赖的package包/类
public void setText(String text) {
    if (text == null || text.length() == 0) {
        setVisibility(GONE);
        return;
    }
    if (text != null && oldText != null && text.equals(oldText)) {
        return;
    }
    oldText = text;
    setVisibility(VISIBLE);
    if (AndroidUtilities.isTablet()) {
        width = (int) (AndroidUtilities.getMinTabletSide() * 0.7f);
    } else {
        width = (int) (Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) * 0.7f);
    }
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
    String help = LocaleController.getString("BotInfoTitle", R.string.BotInfoTitle);
    stringBuilder.append(help);
    stringBuilder.append("\n\n");
    stringBuilder.append(text);
    MessageObject.addLinks(stringBuilder);
    stringBuilder.setSpan(new TypefaceSpan(AndroidUtilities.getTypeface("fonts/rmedium.ttf")), 0, help.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    Emoji.replaceEmoji(stringBuilder, textPaint.getFontMetricsInt(), AndroidUtilities.dp(20), false);
    try {
    textLayout = new StaticLayout(stringBuilder, textPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    width = 0;
    height = textLayout.getHeight() + AndroidUtilities.dp(4 + 18);
    int count = textLayout.getLineCount();
    for (int a = 0; a < count; a++) {
        width = (int) Math.ceil(Math.max(width, textLayout.getLineWidth(a) + textLayout.getLineLeft(a)));
    }
    } catch (Exception e) {
        FileLog.e("tmessage", e);
    }
    width += AndroidUtilities.dp(4 + 18);
}
 
开发者ID:pooyafaroka,项目名称:PlusGram,代码行数:37,代码来源:BotHelpCell.java

示例8: onDraw

import android.text.StaticLayout; //导入方法依赖的package包/类
protected void onDraw(Canvas canvas) {
    if (mCache != null && mStrokeColor != Color.TRANSPARENT) {
        if (mUpdateCachedBitmap) {
            final int w = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
            final int h = getMeasuredHeight();
            final String text = getText().toString();

            mCanvas.setBitmap(mCache);
            mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);

            float strokeWidth = mStrokeWidth > 0 ? mStrokeWidth : (float)Math.ceil(getTextSize() / 11.5f);
            mPaint.setStrokeWidth(strokeWidth);
            mPaint.setColor(mStrokeColor);
            mPaint.setTextSize(getTextSize());
            mPaint.setTypeface(getTypeface());
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

            StaticLayout sl = new StaticLayout(text, mPaint, w, Layout.Alignment.ALIGN_CENTER, 1, 0, true);

            mCanvas.save();
            float a = (h - getPaddingTop() - getPaddingBottom() - sl.getHeight()) / 2.0f;
            mCanvas.translate(getPaddingLeft(), a + getPaddingTop());
            sl.draw(mCanvas);
            mCanvas.restore();

            mUpdateCachedBitmap = false;
        }
        canvas.drawBitmap(mCache, 0, 0, mPaint);
    }
    super.onDraw(canvas);
}
 
开发者ID:pooyafaroka,项目名称:PlusGram,代码行数:32,代码来源:EditTextOutline.java

示例9: getTextHeight

import android.text.StaticLayout; //导入方法依赖的package包/类
private int getTextHeight(CharSequence source, TextPaint paint, int width, float textSize) {
	// modified: make a copy of the original TextPaint object for measuring
	// (apparently the object gets modified while measuring, see also the
	// docs for TextView.getPaint() (which states to access it read-only)
	TextPaint paintCopy = new TextPaint(paint);
	// Update the text paint object
	paintCopy.setTextSize(textSize);
	// Measure using a static layout
	StaticLayout layout = new StaticLayout(source, paintCopy, width, Layout.Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true);
	return layout.getHeight();
}
 
开发者ID:SebastianRask,项目名称:Pocket-Plays-for-Twitch,代码行数:12,代码来源:FontFitTextView.java

示例10: getSize

import android.text.StaticLayout; //导入方法依赖的package包/类
@Override
public int getSize(
        @NonNull Paint paint,
        CharSequence text,
        @IntRange(from = 0) int start,
        @IntRange(from = 0) int end,
        @Nullable Paint.FontMetricsInt fm) {

    // it's our absolute requirement to have width of the canvas here... because, well, it changes
    // the way we draw text. So, if we do not know the width of canvas we cannot correctly measure our text

    if (layouts.size() > 0) {

        if (fm != null) {

            int max = 0;
            for (StaticLayout layout : layouts) {
                final int height = layout.getHeight();
                if (height > max) {
                    max = height;
                }
            }

            // we store actual height
            height = max;

            // but apply height with padding
            final int padding = theme.tableCellPadding() * 2;

            fm.ascent = -(max + padding);
            fm.descent = 0;

            fm.top = fm.ascent;
            fm.bottom = 0;
        }
    }

    return width;
}
 
开发者ID:noties,项目名称:Markwon,代码行数:40,代码来源:TableRowSpan.java

示例11: createMessageBitmap

import android.text.StaticLayout; //导入方法依赖的package包/类
public Bitmap createMessageBitmap(String msg, float fontSize, float width) {
	if (DBG) Log.d(TAG, "createMessageBitmap " + fontSize + " " + width);

	final int textPadding = 20; //MAGICAL

	final int boxWidth = (int)(width+0.5f);
	final int textWidth = boxWidth - 2*textPadding; // remove padding from the width required by user

       StaticLayout textLayout = createTextLayout(msg, Color.WHITE, fontSize, textWidth);

	final int textHeight = textLayout.getHeight();
	final int boxHeight = textHeight + 2*textPadding; // add padding to the text height computed by the layout

	// Allocate the bitmap, must be power of 2 to be used as OpenGL texture
	final int bitmapWidth = getPowerOfTwo(boxWidth);
	final int bitmapHeight = getPowerOfTwo(boxHeight);
	Bitmap destBitmap = Bitmap.createBitmap( bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888 );
	destBitmap.eraseColor(Color.TRANSPARENT);

	synchronized (mCanvas) {
		mCanvas.setBitmap(destBitmap);

		// Draw background
		Paint bgpaint = new Paint();
		bgpaint.setColor(Color.BLACK);
		bgpaint.setAlpha(128);
		final float bgleft = (bitmapWidth-boxWidth)/2;
		final float bgtop = (bitmapHeight-boxHeight)/2;
		mCanvas.translate(bgleft, bgtop);
		RectF windowbg = new RectF( 0, 0, boxWidth, boxHeight);
		mCanvas.drawRoundRect( windowbg, 6f, 6f, bgpaint); //MAGICAL
		mCanvas.translate(-bgleft, -bgtop);

		// Draw message
		final float textleft = (bitmapWidth-textWidth)/2;
		final float texttop = (bitmapHeight-textHeight)/2;
		mCanvas.translate(textleft, texttop);
		textLayout.draw(mCanvas);
		mCanvas.translate(-textleft, -texttop);
	}

	return destBitmap;
}
 
开发者ID:archos-sa,项目名称:aos-MediaLib,代码行数:44,代码来源:ArtworkFactory.java

示例12: createBitmap

import android.text.StaticLayout; //导入方法依赖的package包/类
/**
   * If reuseBmp is not null, and size of the new bitmap matches the size of the reuseBmp,
   * new bitmap won't be created, reuseBmp it will be reused instead
   *
   * @param textLayer text to draw
   * @param reuseBmp  the bitmap that will be reused
   * @return bitmap with the text
   */
  @NonNull
  private Bitmap createBitmap(@NonNull TextLayer textLayer, @Nullable Bitmap reuseBmp) {

    int boundsWidth = canvasWidth;

    // init params - size, color, typeface
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTextSize(textLayer.getFont().getSize() * canvasWidth);
    textPaint.setColor(textLayer.getFont().getColor());
//        textPaint.setTypeface(fontProvider.getTypeface(textLayer.getFont().getTypeface()));

    // drawing text guide : http://ivankocijan.xyz/android-drawing-multiline-text-on-canvas/
    // Static layout which will be drawn on canvas
    StaticLayout sl = new StaticLayout(
        textLayer.getText(), // - text which will be drawn
        textPaint,
        boundsWidth, // - width of the layout
        Layout.Alignment.ALIGN_CENTER, // - layout alignment
        1, // 1 - text spacing multiply
        1, // 1 - text spacing add
        true); // true - include padding

    // calculate height for the entity, min - Limits.MIN_BITMAP_HEIGHT
    int boundsHeight = sl.getHeight();

    // create bitmap not smaller than TextLayer.Limits.MIN_BITMAP_HEIGHT
    int bmpHeight = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
                                                   1.0F * boundsHeight / canvasHeight));

    // create bitmap where text will be drawn
    Bitmap bmp;
    if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
        && reuseBmp.getHeight() == bmpHeight) {
      // if previous bitmap exists, and it's width/height is the same - reuse it
      bmp = reuseBmp;
      bmp.eraseColor(Color.TRANSPARENT); // erase color when reusing
    } else {
      bmp = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bmp);
    canvas.save();

    // move text to center if bitmap is bigger that text
    if (boundsHeight < bmpHeight) {
      //calculate Y coordinate - In this case we want to draw the text in the
      //center of the canvas so we move Y coordinate to center.
      float textYCoordinate = (bmpHeight - boundsHeight) / 2;
      canvas.translate(0, textYCoordinate);
    }

    //draws static layout on canvas
    sl.draw(canvas);
    canvas.restore();

    return bmp;
  }
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:66,代码来源:TextEntity.java

示例13: setTime

import android.text.StaticLayout; //导入方法依赖的package包/类
public void setTime(int value) {
    time = value;

    String timeString;
    if (time >= 1 && time < 60) {
        timeString = "" + value;
        if (timeString.length() < 2) {
            timeString += "s";
        }
    } else if (time >= 60 && time < 60 * 60) {
        timeString = "" + value / 60;
        if (timeString.length() < 2) {
            timeString += "m";
        }
    } else if (time >= 60 * 60 && time < 60 * 60 * 24) {
        timeString = "" + value / 60 / 60;
        if (timeString.length() < 2) {
            timeString += "h";
        }
    } else if (time >= 60 * 60 * 24 && time < 60 * 60 * 24 * 7) {
        timeString = "" + value / 60 / 60 / 24;
        if (timeString.length() < 2) {
            timeString += "d";
        }
    } else {
        timeString = "" + value / 60 / 60 / 24 / 7;
        if (timeString.length() < 2) {
            timeString += "w";
        } else if (timeString.length() > 2) {
            timeString = "c";
        }
    }

    timeWidth = timePaint.measureText(timeString);
    try {
        timeLayout = new StaticLayout(timeString, timePaint, (int)Math.ceil(timeWidth), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        timeHeight = timeLayout.getHeight();
    } catch (Exception e) {
        timeLayout = null;
        FileLog.e("tmessages", e);
    }

    invalidateSelf();
}
 
开发者ID:MLNO,项目名称:airgram,代码行数:45,代码来源:TimerDrawable.java

示例14: onShareButtonClicked

import android.text.StaticLayout; //导入方法依赖的package包/类
/**
 * Share Button Click.
 * get hadith text and create bitmap with hadith text and share it.
 */
private void onShareButtonClicked(String subject, String body) {
    // check if app grant write external storage permission.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // check permission for marshmellow.
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            // Camera permission has not been granted.
            // Camera permission has not been granted yet. Request it directly.
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return;
        }
    }
    // create image from hadith and try share it
    Resources resources = getResources();
    // Create background bitmap
    Bitmap backgroundBitmap = BitmapFactory.decodeResource(resources, R.drawable.backgroundtile);
    Bitmap.Config config = backgroundBitmap.getConfig();
    if (config == null) {
        config = Bitmap.Config.ARGB_8888;
    }

    int width = 600 + (body.length() / 512) * 50;//backgroundBitmap.getWidth();

    // Create logo bitmap
    Bitmap logoBitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher);
    logoBitmap = Bitmap.createScaledBitmap(logoBitmap, 128, 128, false);
    logoBitmap = logoBitmap.copy(config, false);
    int padding = 15;

    // Initiate text paint objects
    TextPaint titleTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
    titleTextPaint.setStyle(Paint.Style.FILL);
    titleTextPaint.setTextSize(28);
    titleTextPaint.setColor(Color.rgb(64, 0, 0));
    titleTextPaint.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/simple.otf"));
    StaticLayout titleStaticLayout = new StaticLayout("منظم المسلم" + "\n" + subject, titleTextPaint, width - 3 * padding - logoBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.4f, 0.1f, false);
    TextPaint matnTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
    matnTextPaint.setStyle(Paint.Style.FILL);
    matnTextPaint.setTextSize(30);
    matnTextPaint.setColor(Color.BLACK);
    matnTextPaint.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/simple.otf"));
    StaticLayout matnStaticLayout = new StaticLayout(body + "\n", matnTextPaint, width - 2 * padding, Layout.Alignment.ALIGN_CENTER, 1.4f, 0.1f, false);
    int height = padding + Math.max(titleStaticLayout.getHeight(), logoBitmap.getHeight()) + padding + matnStaticLayout.getHeight() + padding;

    Bitmap bitmap = backgroundBitmap.copy(config, true);
    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

    // create canvas and draw text on image.
    Canvas canvas = new Canvas(bitmap);
    canvas.save();
    tileBitmap(canvas, backgroundBitmap);
    canvas.drawBitmap(logoBitmap, width - padding - logoBitmap.getWidth(), padding, null);
    canvas.translate(padding, 2 * padding);
    titleStaticLayout.draw(canvas);
    canvas.translate(0, padding + logoBitmap.getHeight());
    matnStaticLayout.draw(canvas);
    canvas.restore();

    // share bitmap.
    shareImage(bitmap);
}
 
开发者ID:fekracomputers,项目名称:MuslimMateAndroid,代码行数:66,代码来源:AzkarActivity.java

示例15: calculateRowHeight

import android.text.StaticLayout; //导入方法依赖的package包/类
private int calculateRowHeight(List<Spanned> row) {

        if (row.size() == 0) {
            return 0;
        }

        TextPaint textPaint = getTextPaint();

        int columnWidth = tableWidth / row.size();

        int rowHeight = 0;

        for (Spanned cell : row) {

            StaticLayout layout = new StaticLayout(cell, textPaint, columnWidth
                    - 2 * PADDING, Alignment.ALIGN_NORMAL, 1.5f, 0.5f, true);

            if (layout.getHeight() > rowHeight) {
                rowHeight = layout.getHeight();
            }
        }

        return rowHeight;
    }
 
开发者ID:duyp,项目名称:mvvm-template,代码行数:25,代码来源:TableHandler.java


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