當前位置: 首頁>>代碼示例>>Java>>正文


Java StaticLayout.draw方法代碼示例

本文整理匯總了Java中android.text.StaticLayout.draw方法的典型用法代碼示例。如果您正苦於以下問題:Java StaticLayout.draw方法的具體用法?Java StaticLayout.draw怎麽用?Java StaticLayout.draw使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.text.StaticLayout的用法示例。


在下文中一共展示了StaticLayout.draw方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: drawCenterMultiText1

import android.text.StaticLayout; //導入方法依賴的package包/類
/**
 * 繪製多行居中文本(方式1)
 *
 * @param canvas 畫布
 */
private void drawCenterMultiText1(Canvas canvas) {
    String text = "ABC";

    // 畫筆
    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);
    textPaint.setColor(Color.GRAY);

    // 設置寬度超過50dp時換行
    StaticLayout staticLayout = new StaticLayout(text, textPaint, dp2px(50),
            Layout.Alignment.ALIGN_CENTER, 1, 0, false);
    canvas.save();
    // StaticLayout默認從(0,0)點開始繪製
    // 如果需要調整位置,隻能在繪製之前移動Canvas的起始坐標
    canvas.translate(-staticLayout.getWidth() / 2, -staticLayout.getHeight() / 2);
    staticLayout.draw(canvas);
    canvas.restore();
}
 
開發者ID:alidili,項目名稱:Demos,代碼行數:24,代碼來源:CustomTextView.java

示例2: getOverlayBitmap

import android.text.StaticLayout; //導入方法依賴的package包/類
public static Bitmap getOverlayBitmap(Context context, Bitmap bitmap, String text) {
    Bitmap result = bitmap.copy(bitmap.getConfig(), true);
    float scale = context.getResources().getDisplayMetrics().density;
    Canvas canvas = new Canvas(result);
    TextPaint mTextPaint = new TextPaint();
    mTextPaint.setTextSize((int) (16 * scale));
    mTextPaint.setColor(Color.RED);
    mTextPaint.setAlpha(38);
    StaticLayout mTextLayout = new StaticLayout(text, mTextPaint, canvas.getWidth() + 700, Layout.Alignment.ALIGN_CENTER, 1.1f, 0.3f, true);
    canvas.save();

    float textX = -200;
    float textY = -10;

    canvas.translate(textX, textY);
    mTextLayout.draw(canvas);
    canvas.restore();
    return result;
}
 
開發者ID:SinsangMarket,項目名稱:ASS,代碼行數:20,代碼來源:Util.java

示例3: drawText

import android.text.StaticLayout; //導入方法依賴的package包/類
private void drawText(Canvas canvas) {
    for (int i = 0; i < maxStep; i++) {
        setPaintColor(i);
        if (null != times && i < proStep)
            canvas.drawText(times[i], bgPositionX - timePaddingRight, stopY - (i * interval) + timeMoveTop, textPaint);
        if (null != titles) {
            canvas.save();
            canvas.translate(bgPositionX + textPaddingLeft, (stopY - (i * interval) - textMoveTop));
            StaticLayout sl = new StaticLayout(titles[i], textPaint, border, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            sl.draw(canvas);
            canvas.restore();
        }
    }
}
 
開發者ID:JJS-CN,項目名稱:JBase,代碼行數:15,代碼來源:FlowViewVertical.java

示例4: draw

import android.text.StaticLayout; //導入方法依賴的package包/類
@Override
protected void draw(Canvas canvas) {
    final float lineHeight = paint.getTextSize();
    float lineY = y;
    final String text = this.text.toString();
    for (CharSequence line : text.split("\n")) {
        canvas.save();
        {
            final float lineWidth = (int) paint.measureText(line.toString());
            float lineX = x;
            //if (alignment == Layout.Alignment.ALIGN_CENTER) {
            //    lineX -= lineWidth / 2f;
            //}
            if (lineX < minXLeft) {
                lineX = minXLeft;
            }

            final float right = lineX + lineWidth;
            if (right > maxXRight) {
                lineX = maxXRight - lineWidth;
            }

            final float width = maxXRight - lineX;

            canvas.translate(lineX, lineY);
            final StaticLayout staticLayout = new StaticLayout(line, paint, (int) width, alignment, 1.0f, 0, false);
            staticLayout.draw(canvas);

            lineY += lineHeight;
        }
        canvas.restore();
    }
}
 
開發者ID:florent37,項目名稱:MyLittleCanvas,代碼行數:34,代碼來源:TextShape.java

示例5: drawMultilineText

import android.text.StaticLayout; //導入方法依賴的package包/類
private void drawMultilineText(Canvas canvas, String text, float x, float y, TextPaint paint, Layout.Alignment aligment) {
    final float lineHeight = paint.getTextSize();
    float lineY = y;
    for (CharSequence line : text.split("\n")) {
        canvas.save();
        {
            final float lineWidth = (int) paint.measureText(line.toString());
            float lineX = x;
            if (aligment == Layout.Alignment.ALIGN_CENTER) {
                lineX -= lineWidth / 2f;
            }
            if (lineX < 0) {
                lineX = 0;
            }

            final float right = lineX + lineWidth;
            if (right > canvas.getWidth()) {
                lineX = canvas.getWidth() - lineWidth - settings.paddingCorners;
            }

            canvas.translate(lineX, lineY);
            final StaticLayout staticLayout = new StaticLayout(line, paint, (int) lineWidth, aligment, 1.0f, 0, false);
            staticLayout.draw(canvas);

            lineY += lineHeight;
        }
        canvas.restore();
    }

}
 
開發者ID:florent37,項目名稱:android-slidr,代碼行數:31,代碼來源:Slidr.java

示例6: draw

import android.text.StaticLayout; //導入方法依賴的package包/類
@Override
public void draw(Canvas canvas) {
	Paint paint = new Paint();
	paint.setColor(textColor);
	paint.setStyle(Style.STROKE);

	int numberOfColumns = tableRow.size();

	if (numberOfColumns == 0) {
		return;
	}

	int columnWidth = tableWidth / numberOfColumns;

	int offset = 0;

	for (int i = 0; i < numberOfColumns; i++) {

		offset = i * columnWidth;

              if ( paintBorder ) {
		    // The rect is open at the bottom, so there's a single line
		    // between rows.
		    canvas.drawRect(offset, 0, offset + columnWidth, rowHeight,
				paint);
              }

		StaticLayout layout = new StaticLayout(tableRow.get(i),
				getTextPaint(), (columnWidth - 2 * PADDING),
				Alignment.ALIGN_NORMAL, 1f, 0f, true);

		canvas.translate(offset + PADDING, 0);
		layout.draw(canvas);
		canvas.translate(-1 * (offset + PADDING), 0);

	}
}
 
開發者ID:SysdataSpA,項目名稱:SDHtmlTextView,代碼行數:38,代碼來源:TableHandler.java

示例7: drawText

import android.text.StaticLayout; //導入方法依賴的package包/類
private void drawText(Canvas canvas, String text, float x, float y, TextPaint paint, Layout.Alignment aligment) {
    canvas.save();
    {
        canvas.translate(x, y);
        final StaticLayout staticLayout = new StaticLayout(text, paint, (int) paint.measureText(text), aligment, 1.0f, 0, false);
        staticLayout.draw(canvas);
    }
    canvas.restore();
}
 
開發者ID:florent37,項目名稱:android-slidr,代碼行數:10,代碼來源:Sushi.java

示例8: drawTextInfo

import android.text.StaticLayout; //導入方法依賴的package包/類
private void drawTextInfo(Canvas canvas, Rect frame) {
    paint.setColor(labelTextColor);
    paint.setTextSize(labelTextSize);
    paint.setTextAlign(Paint.Align.CENTER);
    canvas.save();
    mTipTextSl = new StaticLayout(labelText, labTextPaint, DisplayUtil.getScreenResolution(getContext()).x,
            Layout.Alignment.ALIGN_CENTER, 1.0f, 0, true);
    canvas.translate(0,
            frame.bottom +mTipTextSl.getHeight());
    mTipTextSl.draw(canvas);
    canvas.restore();
}
 
開發者ID:RuanXiaoHui,項目名稱:ZxingScan,代碼行數:13,代碼來源:ViewfinderView.java

示例9: 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

示例10: drawTextLayout

import android.text.StaticLayout; //導入方法依賴的package包/類
private void drawTextLayout(Canvas canvas) {
  StaticLayout layout = textLayout;
  if (layout == null) {
    // Nothing to draw.
    return;
  }

  int saveCount = canvas.save();
  canvas.translate(textLeft, textTop);

  if (Color.alpha(windowColor) > 0) {
    paint.setColor(windowColor);
    canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
        paint);
  }

  if (Color.alpha(backgroundColor) > 0) {
    paint.setColor(backgroundColor);
    float previousBottom = layout.getLineTop(0);
    int lineCount = layout.getLineCount();
    for (int i = 0; i < lineCount; i++) {
      lineBounds.left = layout.getLineLeft(i) - textPaddingX;
      lineBounds.right = layout.getLineRight(i) + textPaddingX;
      lineBounds.top = previousBottom;
      lineBounds.bottom = layout.getLineBottom(i);
      previousBottom = lineBounds.bottom;
      canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
    }
  }

  if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
    textPaint.setStrokeJoin(Join.ROUND);
    textPaint.setStrokeWidth(outlineWidth);
    textPaint.setColor(edgeColor);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    layout.draw(canvas);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
    textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
      || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
    boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
    int colorUp = raised ? Color.WHITE : edgeColor;
    int colorDown = raised ? edgeColor : Color.WHITE;
    float offset = shadowRadius / 2f;
    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
    layout.draw(canvas);
    textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
  }

  textPaint.setColor(foregroundColor);
  textPaint.setStyle(Style.FILL);
  layout.draw(canvas);
  textPaint.setShadowLayer(0, 0, 0, 0);

  canvas.restoreToCount(saveCount);
}
 
開發者ID:yangchaojiang,項目名稱:yjPlay,代碼行數:59,代碼來源:SubtitlePainter.java

示例11: drawMultilineText

import android.text.StaticLayout; //導入方法依賴的package包/類
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:77,代碼來源:Utils.java

示例12: calcuPageOffsetOneChapter

import android.text.StaticLayout; //導入方法依賴的package包/類
private void calcuPageOffsetOneChapter(List<Content> chapter) {

        int boundedWidth = (int) mWidth;
        StaticLayout layout = StaticLayoutFactory.create(spannableStringBuilder,
                PagetSizeConfig.getPaint(), boundedWidth, PagetSizeConfig.getLineSpacing());
        layout.draw(new Canvas());

        int pageHeight = (int) PagetSizeConfig.getPageHeight(mBookView.getContext());
        Log.d(TAG, "Calcu page width = " + boundedWidth + ", height = " + pageHeight);


        int totalLines = layout.getLineCount();
        int topLineNextPage = -1;
        int pageStartOffset = 0;

        while (topLineNextPage < totalLines - 1) {

            Log.d(TAG, "Processing line " + topLineNextPage + " / " + totalLines);

            int topLine = layout.getLineForOffset(pageStartOffset);
            topLineNextPage = layout.getLineForVertical(layout.getLineTop(topLine) + pageHeight);

            Log.d(TAG, "topLine " + topLine + " / " + topLineNextPage);
            if (topLineNextPage == topLine) {
                //If lines are bigger than can fit on a page
                topLineNextPage = topLine + 1;
            }

            int pageEnd = layout.getLineEnd(topLineNextPage - 1);

            Log.d(TAG, "pageStartOffset=" + pageStartOffset + ", pageEnd=" + pageEnd);

            if (pageEnd > pageStartOffset) {
                if (spannableStringBuilder.subSequence(pageStartOffset, pageEnd).toString().trim().length() > 0) {
                    PageIndex pageIndex = findPageIndexByOffset(chapter, pageStartOffset);
                    Log.d(TAG, "add pageIndex :" + pageIndex.getIndex());

                    mPageIndices.add(pageIndex);
                }
                pageStartOffset = layout.getLineStart(topLineNextPage);
            }
        }

    }
 
開發者ID:ceji-longquan,項目名稱:ceji_android,代碼行數:45,代碼來源:PageCounter.java

示例13: 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

示例14: onDraw

import android.text.StaticLayout; //導入方法依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
    if (titleLayout != null) {
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline), titleY);
        titleLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout != null) {
        descriptionTextPaint.setColor(0xff212121);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline), descriptionY);
        descriptionLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout2 != null) {
        descriptionTextPaint.setColor(0xff212121);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline), description2Y);
        descriptionLayout2.draw(canvas);
        canvas.restore();
    }

    if (!linkLayout.isEmpty()) {
        descriptionTextPaint.setColor(Theme.MSG_LINK_TEXT_COLOR);
        int offset = 0;
        for (int a = 0; a < linkLayout.size(); a++) {
            StaticLayout layout = linkLayout.get(a);
            if (layout.getLineCount() > 0) {
                canvas.save();
                canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline), linkY + offset);
                if (pressedLink == a) {
                    canvas.drawPath(urlPath, urlPaint);
                }
                layout.draw(canvas);
                canvas.restore();
                offset += layout.getLineBottom(layout.getLineCount() - 1);
            }
        }
    }

    letterDrawable.draw(canvas);
    if (drawLinkImageView) {
        linkImageView.draw(canvas);
    }

    if (needDivider) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1, getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1, paint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1, getMeasuredWidth(), getMeasuredHeight() - 1, paint);
        }
    }
}
 
開發者ID:MLNO,項目名稱:airgram,代碼行數:57,代碼來源:SharedLinkCell.java

示例15: onDraw

import android.text.StaticLayout; //導入方法依賴的package包/類
@Override
protected void onDraw(Canvas canvas) {
    if (letters.isEmpty()) {
        return;
    }
    float height = letters.get(0).getHeight();
    canvas.save();
    canvas.translate(getPaddingLeft(), (getMeasuredHeight() - height) / 2);
    int count = Math.max(letters.size(), oldLetters.size());
    for (int a = 0; a < count; a++) {
        canvas.save();
        StaticLayout old = a < oldLetters.size() ? oldLetters.get(a) : null;
        StaticLayout layout = a < letters.size() ? letters.get(a) : null;
        if (progress > 0) {
            if (old != null) {
                textPaint.setAlpha((int) (255 * progress));
                canvas.save();
                canvas.translate(0, (progress - 1.0f) * height);
                old.draw(canvas);
                canvas.restore();
                if (layout != null) {
                    textPaint.setAlpha((int) (255 * (1.0f - progress)));
                    canvas.translate(0, progress * height);
                }
            } else {
                textPaint.setAlpha(255);
            }
        } else if (progress < 0) {
            if (old != null) {
                textPaint.setAlpha((int) (255 * -progress));
                canvas.save();
                canvas.translate(0, (1.0f + progress) * height);
                old.draw(canvas);
                canvas.restore();
            }
            if (layout != null) {
                if (a == count - 1 || old != null) {
                    textPaint.setAlpha((int) (255 * (1.0f + progress)));
                    canvas.translate(0, progress * height);
                } else {
                    textPaint.setAlpha(255);
                }
            }
        } else if (layout != null) {
            textPaint.setAlpha(255);
        }
        if (layout != null) {
            layout.draw(canvas);
        }
        canvas.restore();
        canvas.translate(layout != null ? layout.getLineWidth(0) : old.getLineWidth(0) + AndroidUtilities.dp(1), 0);
    }
    canvas.restore();
}
 
開發者ID:MLNO,項目名稱:airgram,代碼行數:55,代碼來源:NumberTextView.java


注:本文中的android.text.StaticLayout.draw方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。