当前位置: 首页>>代码示例>>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;未经允许,请勿转载。