List of usage examples for android.text StaticLayout draw
public void draw(Canvas c)
From source file:Main.java
public static int drawStaticLayout(Canvas canvas, StaticLayout layout, int x, int y) { canvas.translate(x, y);/*from w w w .j av a 2 s . c o m*/ layout.draw(canvas); canvas.translate(-x, -y); return layout.getHeight(); }
From source file:org.wikipedia.page.shareafact.SnippetImage.java
@NonNull private static Layout drawTextSnippet(@NonNull Canvas canvas, @NonNull CharSequence textSnippet) { final int top = TOP_PADDING; final int maxHeight = 225; final int maxLines = 5; final float maxFontSize = 195.0f; final float minFontSize = 32.0f; TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true);//from ww w. j av a 2s. c o m textPaint.setColor(Color.WHITE); textPaint.setTextSize(maxFontSize); textPaint.setStyle(Paint.Style.FILL); textPaint.setTypeface(Typeface.DEFAULT_BOLD); textPaint.setShadowLayer(1.0f, 1.0f, 1.0f, Color.GRAY); StaticLayout textLayout = optimizeTextSize( new TextLayoutParams(textSnippet, textPaint, TEXT_WIDTH, SPACING_MULTIPLIER), maxHeight, maxLines, maxFontSize, minFontSize); canvas.save(); int horizontalCenterOffset = top + (maxHeight - textLayout.getHeight()) / QUARTER; canvas.translate(HORIZONTAL_PADDING, horizontalCenterOffset); textLayout.draw(canvas); canvas.restore(); return textLayout; }
From source file:org.wikipedia.page.shareafact.SnippetImage.java
private static int drawDescription(@NonNull Canvas canvas, @Nullable String description, int top, boolean isArticleRTL) { final int marginBottom = 5; final int maxHeight = 23; final int maxLines = 2; final float maxFontSize = 15.0f; final float minFontSize = 10.0f; if (TextUtils.isEmpty(description)) { return top - marginBottom; }//w ww .j a v a 2 s . co m TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true); textPaint.setColor(Color.WHITE); textPaint.setTextSize(maxFontSize); textPaint.setStyle(Paint.Style.FILL); textPaint.setShadowLayer(1.0f, 0.0f, 0.0f, Color.GRAY); StaticLayout textLayout = optimizeTextSize( new TextLayoutParams(description, textPaint, DESCRIPTION_WIDTH, SPACING_MULTIPLIER), maxHeight, maxLines, maxFontSize, minFontSize); int left = HORIZONTAL_PADDING; if (isArticleRTL) { left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth(); } top = top - marginBottom - textLayout.getHeight(); canvas.save(); canvas.translate(left, top); textLayout.draw(canvas); canvas.restore(); return top; }
From source file:org.wikipedia.page.shareafact.SnippetImage.java
private static void drawWordmarkFromText(@NonNull Context context, @NonNull Canvas canvas, boolean isArticleRTL) { final int maxWidth = WIDTH - DESCRIPTION_WIDTH - 2 * HORIZONTAL_PADDING; final float fontSize = 20.0f; final float scaleX = 1.06f; TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true);/*w w w . j a v a 2 s .c o m*/ textPaint.setColor(Color.LTGRAY); textPaint.setTextSize(fontSize); textPaint.setTypeface(SERIF); textPaint.setTextScaleX(scaleX); Spanned wikipedia = StringUtil.fromHtml(context.getString(R.string.wp_stylized)); Layout.Alignment align = L10nUtil.isDeviceRTL() ? ALIGN_OPPOSITE : ALIGN_NORMAL; StaticLayout wordmarkLayout = buildLayout( new TextLayoutParams(wikipedia, textPaint, maxWidth, 1.0f, align)); final int width = (int) wordmarkLayout.getLineWidth(0); final int height = wordmarkLayout.getHeight(); final int bottom = HEIGHT - BOTTOM_PADDING; final int top = bottom - height; int left = WIDTH - HORIZONTAL_PADDING - width; if (isArticleRTL) { left = HORIZONTAL_PADDING; } canvas.save(); // -- canvas.translate(left, top); wordmarkLayout.draw(canvas); canvas.restore(); // -- }
From source file:org.wikipedia.page.shareafact.SnippetImage.java
private static void drawTitle(@NonNull Canvas canvas, @NonNull String title, int top, boolean isArticleRTL) { final int marginBottom = 0; final int maxHeight = 70; final int maxLines = 2; final float maxFontSize = 30.0f; final float spacingMultiplier = 0.7f; TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true);/*from w w w .j a v a2s . c om*/ textPaint.setColor(Color.WHITE); textPaint.setTextSize(maxFontSize); textPaint.setStyle(Paint.Style.FILL); textPaint.setTypeface(SERIF); textPaint.setShadowLayer(1.0f, 0.0f, 1.0f, Color.GRAY); StaticLayout textLayout = optimizeTextSize( new TextLayoutParams(title, textPaint, DESCRIPTION_WIDTH, spacingMultiplier), maxHeight, maxLines, maxFontSize, maxFontSize); int left = HORIZONTAL_PADDING; if (isArticleRTL) { left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth(); } int marginBottomTotal = marginBottom; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // versions < 5.0 don't compensate for bottom margin correctly when line // spacing is less than 1.0, so we'll compensate ourselves final int marginBoost = 10; marginBottomTotal += marginBoost; } top = top - marginBottomTotal - textLayout.getHeight(); canvas.save(); canvas.translate(left, top); textLayout.draw(canvas); canvas.restore(); }
From source file:com.achillesrasquinha.biblegenerator.ImageGenerator.java
public Bitmap getBitmap(String title, String subtitle, String text) { Bitmap bitmap = Bitmap.createBitmap(mImageSize.x, mImageSize.y, Bitmap.Config.ARGB_8888); //convert bitmap to card view background color bitmap.eraseColor(ContextCompat.getColor(mContext, R.color.cardview_light_background)); Canvas canvas = new Canvas(bitmap); //title/* ww w. j a v a2 s .c o m*/ mTextPaint.setTextSize(mSizeTitle); mTextPaint.setColor(mColor1); canvas.drawText(title, mX, mYTitle, mTextPaint); //subtitle mTextPaint.setTextSize(mSizeSubtitle); mTextPaint.setColor(mColor2); canvas.drawText(subtitle, mX, mYSubtitle, mTextPaint); //since text needs to be wrapped, using a StaticLayout mTextPaint.setColor(mColor3); //text size same as subtitle size StaticLayout layout = new StaticLayout(text, mTextPaint, mImageSize.x - 2 * mX, Layout.Alignment.ALIGN_NORMAL, 1.0f, mLineSpacing, false); //16dp padding as per material design guidelines canvas.translate(mX, mYSubtitle + mX); layout.draw(canvas); return bitmap; }
From source file:com.ifoer.util.NetPOSPrinter.java
public Bitmap drawBitSecond(String s) { if (this.company_address == null) { this.company_address = XmlPullParser.NO_NAMESPACE; }//from w w w . j ava2 s. c o m if (this.company_phone == null) { this.company_phone = XmlPullParser.NO_NAMESPACE; } if (this.license_plate_number == null) { this.license_plate_number = XmlPullParser.NO_NAMESPACE; } StringBuffer ss = new StringBuffer(); ss.append(new StringBuilder( String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_time))) .append(this.str).append(SpecilApiUtil.LINE_SEP).toString()); ss.append(new StringBuilder( String.valueOf(this.mContext.getResources().getString(C0136R.string.print_serial_number))) .append(this.serialNum).append(SpecilApiUtil.LINE_SEP).toString()); ss.append(new StringBuilder( String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_address))) .append(this.company_address).append(SpecilApiUtil.LINE_SEP).toString()); ss.append(new StringBuilder( String.valueOf(this.mContext.getResources().getString(C0136R.string.print_test_company_phone))) .append(this.company_phone).append(SpecilApiUtil.LINE_SEP).toString()); ss.append(new StringBuilder(String .valueOf(this.mContext.getResources().getString(C0136R.string.print_test_license_plate_number))) .append(this.license_plate_number).append(SpecilApiUtil.LINE_SEP).toString()); ss.append(s); this.textPaint.setColor(DefaultRenderer.BACKGROUND_COLOR); this.textPaint.setTextSize(20.0f); StaticLayout layout = new StaticLayout(ss, this.textPaint, PRINT_WIDTH, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true); this.nBitmapSecond = Bitmap.createBitmap(PRINT_WIDTH, layout.getHeight(), Config.RGB_565); Canvas canvas = new Canvas(this.nBitmapSecond); canvas.drawColor(-1); layout.draw(canvas); return this.nBitmapSecond; }
From source file:com.anysoftkeyboard.keyboards.views.CandidateView.java
/** * If the canvas is null, then only touch calculations are performed to pick * the target candidate./*from www .j av a 2 s .c om*/ */ @Override protected void onDraw(Canvas canvas) { if (canvas != null) { super.onDraw(canvas); } mTotalWidth = 0; final int height = getHeight(); if (mBgPadding == null) { mBgPadding = new Rect(0, 0, 0, 0); if (getBackground() != null) { getBackground().getPadding(mBgPadding); } mDivider.setBounds(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); } final int dividerYOffset = (height - mDivider.getMinimumHeight()) / 2; final int count = mSuggestions.size(); final Rect bgPadding = mBgPadding; final Paint paint = mPaint; final int touchX = mTouchX; final int scrollX = getScrollX(); final boolean scrolled = mScrolled; final boolean typedWordValid = mTypedWordValid; int x = 0; for (int i = 0; i < count; i++) { CharSequence suggestion = mSuggestions.get(i); if (suggestion == null) continue; final int wordLength = suggestion.length(); paint.setColor(mColorNormal); if (mHaveMinimalSuggestion && ((i == 1 && !typedWordValid) || (i == 0 && typedWordValid))) { paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setColor(mColorRecommended); // existsAutoCompletion = true; } else if (i != 0 || (wordLength == 1 && count > 1)) { // HACK: even if i == 0, we use mColorOther when this // suggestion's length is 1 and // there are multiple suggestions, such as the default // punctuation list. paint.setColor(mColorOther); } // now that we set the typeFace, we can measure int wordWidth; if ((wordWidth = mWordWidth[i]) == 0) { float textWidth = paint.measureText(suggestion, 0, wordLength); // wordWidth = Math.max(0, (int) textWidth + X_GAP * 2); wordWidth = (int) (textWidth + mHorizontalGap * 2); mWordWidth[i] = wordWidth; } mWordX[i] = x; if (touchX != OUT_OF_BOUNDS_X_CORD && !scrolled && touchX + scrollX >= x && touchX + scrollX < x + wordWidth) { if (canvas != null && !mShowingAddToDictionary) { canvas.translate(x, 0); mSelectionHighlight.setBounds(0, bgPadding.top, wordWidth, height); mSelectionHighlight.draw(canvas); canvas.translate(-x, 0); } mSelectedString = suggestion; mSelectedIndex = i; } if (canvas != null) { // (+)This is the trick to get RTL/LTR text correct if (AnyApplication.getConfig().workaround_alwaysUseDrawText()) { final int y = (int) (height + paint.getTextSize() - paint.descent()) / 2; canvas.drawText(suggestion, 0, wordLength, x + wordWidth / 2, y, paint); } else { final int y = (int) (height - paint.getTextSize() + paint.descent()) / 2; // no matter what: StaticLayout float textX = x + (wordWidth / 2) - mHorizontalGap; float textY = y - bgPadding.bottom - bgPadding.top; canvas.translate(textX, textY); mTextPaint.setTypeface(paint.getTypeface()); mTextPaint.setColor(paint.getColor()); StaticLayout suggestionText = new StaticLayout(suggestion, mTextPaint, wordWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); suggestionText.draw(canvas); canvas.translate(-textX, -textY); } // (-) paint.setColor(mColorOther); canvas.translate(x + wordWidth, 0); // Draw a divider unless it's after the hint //or the last suggested word if (count > 1 && (!mShowingAddToDictionary) && i != (count - 1)) { canvas.translate(0, dividerYOffset); mDivider.draw(canvas); canvas.translate(0, -dividerYOffset); } canvas.translate(-x - wordWidth, 0); } paint.setTypeface(Typeface.DEFAULT); x += wordWidth; } mTotalWidth = x; if (mTargetScrollX != scrollX) { scrollToTarget(); } }
From source file:com.yek.keyboard.keyboards.views.CandidateView.java
/** * If the canvas is null, then only touch calculations are performed to pick * the target candidate./*from www . ja v a 2 s . c om*/ */ @Override protected void onDraw(Canvas canvas) { if (canvas != null) { super.onDraw(canvas); } mTotalWidth = 0; final int height = getHeight(); if (mBgPadding == null) { mBgPadding = new Rect(0, 0, 0, 0); if (getBackground() != null) { getBackground().getPadding(mBgPadding); } mDivider.setBounds(0, 0, mDivider.getIntrinsicWidth(), mDivider.getIntrinsicHeight()); } final int dividerYOffset = (height - mDivider.getMinimumHeight()) / 2; final int count = mSuggestions.size(); final Rect bgPadding = mBgPadding; final Paint paint = mPaint; final int touchX = mTouchX; final int scrollX = getScrollX(); final boolean scrolled = mScrolled; final boolean typedWordValid = mTypedWordValid; int x = 0; for (int i = 0; i < count; i++) { CharSequence suggestion = mSuggestions.get(i); if (suggestion == null) continue; final int wordLength = suggestion.length(); paint.setColor(mColorNormal); if (mHaveMinimalSuggestion && ((i == 1 && !typedWordValid) || (i == 0 && typedWordValid))) { paint.setTypeface(Typeface.DEFAULT_BOLD); paint.setColor(mColorRecommended); // existsAutoCompletion = true; } else if (i != 0 || (wordLength == 1 && count > 1)) { // HACK: even if i == 0, we use mColorOther when this // suggestion's length is 1 and // there are multiple suggestions, such as the default // punctuation list. paint.setColor(mColorOther); } // now that we set the typeFace, we can measure int wordWidth; if ((wordWidth = mWordWidth[i]) == 0) { float textWidth = paint.measureText(suggestion, 0, wordLength); // wordWidth = Math.max(0, (int) textWidth + X_GAP * 2); wordWidth = (int) (textWidth + mXGap * 2); mWordWidth[i] = wordWidth; } mWordX[i] = x; if (touchX != OUT_OF_BOUNDS_X_CORD && !scrolled && touchX + scrollX >= x && touchX + scrollX < x + wordWidth) { if (canvas != null && !mShowingAddToDictionary) { canvas.translate(x, 0); mSelectionHighlight.setBounds(0, bgPadding.top, wordWidth, height); mSelectionHighlight.draw(canvas); canvas.translate(-x, 0); } mSelectedString = suggestion; mSelectedIndex = i; } if (canvas != null) { // (+)This is the trick to get RTL/LTR text correct if (AnyApplication.getConfig().workaround_alwaysUseDrawText()) { final int y = (int) (height + paint.getTextSize() - paint.descent()) / 2; canvas.drawText(suggestion, 0, wordLength, x + wordWidth / 2, y, paint); } else { final int y = (int) (height - paint.getTextSize() + paint.descent()) / 2; // no matter what: StaticLayout float textX = x + (wordWidth / 2) - mXGap; float textY = y - bgPadding.bottom - bgPadding.top; canvas.translate(textX, textY); mTextPaint.setTypeface(paint.getTypeface()); mTextPaint.setColor(paint.getColor()); StaticLayout suggestionText = new StaticLayout(suggestion, mTextPaint, wordWidth, Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); suggestionText.draw(canvas); canvas.translate(-textX, -textY); } // (-) paint.setColor(mColorOther); canvas.translate(x + wordWidth, 0); // Draw a divider unless it's after the hint //or the last suggested word if (count > 1 && (!mShowingAddToDictionary) && i != (count - 1)) { canvas.translate(0, dividerYOffset); mDivider.draw(canvas); canvas.translate(0, -dividerYOffset); } canvas.translate(-x - wordWidth, 0); } paint.setTypeface(Typeface.DEFAULT); x += wordWidth; } mTotalWidth = x; if (mTargetScrollX != scrollX) { scrollToTarget(); } }
From source file:com.semfapp.adamdilger.semf.Take5PdfDocument.java
public void drawText(String string, float xLoc, float yLoc, float textSize, Typeface typeface) { if (string != null) { TextPaint mTextPaint = new TextPaint(); mTextPaint.setTypeface(typeface); mTextPaint.setTextSize(textSize); StaticLayout mTextLayout = new StaticLayout(string, mTextPaint, ((int) (595 * 0.28)), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); can.save();//from w ww.ja va2 s. c om // calculate x and y position where your text will be placed can.translate(xLoc, yLoc); mTextLayout.draw(can); can.restore(); } }