Example usage for android.text TextPaint setTypeface

List of usage examples for android.text TextPaint setTypeface

Introduction

In this page you can find the example usage for android.text TextPaint setTypeface.

Prototype

public Typeface setTypeface(Typeface typeface) 

Source Link

Document

Set or clear the typeface object.

Usage

From source file:se.attentec.attenhome.TypefaceSpan.java

@Override
public void updateDrawState(TextPaint tp) {
    tp.setTypeface(mTypeface);
    tp.setTextSize(mTextSize);/*from   ww  w .  ja  v  a  2s.  c  om*/
    tp.setColor(mColor);
    // Note: This flag is required for proper typeface rendering
    tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

From source file:com.grarak.kerneladiutor.elements.SplashView.java

private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0)
        canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);//  w ww .j a  v a  2s. c  o m
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2,
            mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}

From source file:com.mobisci_lab.virtualkeyboard.softkeyboard.LatinKeyboardView.java

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    TextPaint paint = new TextPaint();
    paint.setAntiAlias(true);//from  www. ja va 2s .  com
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    paint.setTextSize(11 * getResources().getDisplayMetrics().density);
    paint.setColor(ContextCompat.getColor(this.getContext(), R.color.candidate_other));

    List<Key> keys = getKeyboard().getKeys();
    for (Key key : keys) {
        float x = key.x + key.width / 2 + key.width / 5;
        float y = key.y + key.width / 2;
        String text = "";
        switch (key.codes[0]) {
        case 113:
            text = "1";
            break;
        case 119:
            text = "2";
            break;
        case 101:
            text = "3";
            break;
        case 114:
            text = "4";
            break;
        case 116:
            text = "5";
            break;
        case 121:
            text = "6";
            break;
        case 117:
            text = "7";
            break;
        case 105:
            text = "8";
            break;
        case 111:
            text = "9";
            break;
        case 112:
            text = "0";
            break;

        }
        canvas.drawText(text, x, y, paint);
    }
}

From source file:com.raspi.chatapp.util.Notification.java

private Bitmap getLargeIcon(int bgColor, char letter, float width, boolean round) {
    Bitmap b = Bitmap.createBitmap((int) width, (int) width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    RectF mInnerRectF = new RectF();
    mInnerRectF.set(0, 0, width, width);
    mInnerRectF.offset(0, 0);//from w  ww  . ja  v  a2s  .com

    Paint mBgPaint = new Paint();
    mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setStyle(Paint.Style.FILL);
    mBgPaint.setColor(bgColor);

    TextPaint mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(Color.WHITE);
    mTitleTextPaint.setTextSize(width * 0.8f);

    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    int xPos = (int) centerX;
    int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);

    if (round)
        c.drawOval(mInnerRectF, mBgPaint);
    else
        c.drawRect(mInnerRectF, mBgPaint);
    c.drawText(String.valueOf(letter), xPos, yPos, mTitleTextPaint);

    return b;
}

From source file:com.quran.labs.androidquran.widgets.SlidingTabLayout.java

private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final View.OnClickListener tabClickListener = new TabClickListener();

    final DisplayMetrics metrics = getResources().getDisplayMetrics();
    final float fontSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP,
            metrics);//from w  w w. j a  va 2  s.  co  m
    final TextPaint paint = new TextPaint();
    paint.setTextSize(fontSize);
    paint.setTypeface(Typeface.DEFAULT_BOLD);

    int targetWidth = 0;
    final int tabs = adapter.getCount();
    for (int i = 0; i < tabs; i++) {
        String str = adapter.getPageTitle(i).toString();
        str = str.toUpperCase(Locale.getDefault());

        int width = (int) paint.measureText(str);
        width = width + 2 * mTabPadding;
        if (width > targetWidth) {
            targetWidth = width;
        }
    }

    if (targetWidth * tabs < metrics.widthPixels) {
        targetWidth = metrics.widthPixels / tabs;
    }

    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;
        TextView tabTitleView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        if (tabTitleView != null) {
            tabTitleView.setText(adapter.getPageTitle(i));
        }
        tabView.setOnClickListener(tabClickListener);

        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(targetWidth,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        mTabStrip.addView(tabView, params);
    }
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.draw(imageCanvas);/*  w  ww.j a v a 2  s  . c o m*/

    // Set up the paint for use with our Canvas
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
    textPaint.setTextAlign(TextPaint.Align.CENTER);
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setTextSize(100f);
    textPaint.setColor(context.getResources().getColor(android.R.color.white));

    int xPos = (imageCanvas.getWidth() / 2);
    int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
    Rect r = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), r);
    //        yPos += (Math.abs(r.height()))/2;

    // Draw the text on top of our image
    imageCanvas.drawText(text, xPos, yPos, textPaint);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

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();/* ww w.j  av a 2  s. c o  m*/
        // calculate x and y position where your text will be placed

        can.translate(xLoc, yLoc);
        mTextLayout.draw(can);
        can.restore();
    }
}

From source file:com.semfapp.adamdilger.semf.Take5PdfDocument.java

public float drawRiskElement(int height, Take5RiskElement element) {
    final float singleLineHeight = 12.5f;
    float totalItemHeight;

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(roboto);
    textPaint.setTextSize(FONT11);/*from   w w w. j a  va 2 s.  c  o m*/
    textPaint.setColor(Color.BLACK);

    String oneString = "";
    String twoString = "";

    if (element.getOne() != null) {
        oneString = element.getOne();
    }
    if (element.getTwo() != null) {
        twoString = element.getTwo();
    }

    StaticLayout one = new StaticLayout(oneString, textPaint, 235, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            false);
    StaticLayout rating = new StaticLayout(element.getRating().toString(), textPaint, 100,
            Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    StaticLayout two = new StaticLayout(twoString, textPaint, 250, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
            false);

    if (one.getLineCount() > two.getLineCount()) {
        totalItemHeight = singleLineHeight * one.getLineCount();
    } else {
        totalItemHeight = singleLineHeight * two.getLineCount();
    }

    can.save();
    can.translate(15, height);
    one.draw(can);
    can.restore();

    can.save();
    can.translate(320, height);
    two.draw(can);
    can.restore();

    can.save();
    can.translate(270, height);
    rating.draw(can);
    can.restore();

    return totalItemHeight;
}

From source file:io.plaidapp.core.ui.transitions.ReflowText.java

private Layout createLayout(ReflowData data, Context context, boolean enforceMaxLines) {
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextSize(data.textSize);//w w  w  .  j av  a 2  s .c o m
    paint.setColor(data.textColor);
    paint.setLetterSpacing(data.letterSpacing);
    if (data.fontResId != 0) {
        try {
            Typeface font = ResourcesCompat.getFont(context, data.fontResId);
            if (font != null) {
                paint.setTypeface(font);
            }
        } catch (Resources.NotFoundException nfe) {
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StaticLayout.Builder builder = StaticLayout.Builder
                .obtain(data.text, 0, data.text.length(), paint, data.textWidth)
                .setLineSpacing(data.lineSpacingAdd, data.lineSpacingMult).setBreakStrategy(data.breakStrategy);
        if (enforceMaxLines && data.maxLines != -1) {
            builder.setMaxLines(data.maxLines);
            builder.setEllipsize(TextUtils.TruncateAt.END);
        }
        return builder.build();
    } else {
        return new StaticLayout(data.text, paint, data.textWidth, Layout.Alignment.ALIGN_NORMAL,
                data.lineSpacingMult, data.lineSpacingAdd, true);
    }
}

From source file:org.telegram.ui.ArticleViewer.java

private TextPaint getTextPaint(TLRPC.RichText parentRichText, TLRPC.RichText richText,
        TLRPC.PageBlock parentBlock) {//from w  w  w .j  av  a2 s  . c  o  m
    int flags = getTextFlags(richText);
    HashMap<Integer, TextPaint> currentMap = null;
    int textSize = AndroidUtilities.dp(14);
    int textColor = 0xffff0000;

    if (parentBlock instanceof TLRPC.TL_pageBlockPhoto) {
        currentMap = captionTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockTitle) {
        currentMap = titleTextPaints;
        textSize = AndroidUtilities.dp(24);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockAuthorDate) {
        currentMap = authorTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockFooter) {
        currentMap = footerTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSubtitle) {
        currentMap = subtitleTextPaints;
        textSize = AndroidUtilities.dp(21);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockHeader) {
        currentMap = headerTextPaints;
        textSize = AndroidUtilities.dp(21);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSubheader) {
        currentMap = subheaderTextPaints;
        textSize = AndroidUtilities.dp(18);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockBlockquote
            || parentBlock instanceof TLRPC.TL_pageBlockPullquote) {
        if (parentBlock.text == parentRichText) {
            currentMap = quoteTextPaints;
            textSize = AndroidUtilities.dp(15);
            textColor = 0xff000000;
        } else if (parentBlock.caption == parentRichText) {
            currentMap = subquoteTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff838c96;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockPreformatted) {
        currentMap = preformattedTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockParagraph) {
        if (parentBlock.caption == parentRichText) {
            currentMap = embedPostCaptionTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff838c96;
        } else {
            currentMap = paragraphTextPaints;
            textSize = AndroidUtilities.dp(16);
            textColor = 0xff000000;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockList) {
        currentMap = listTextPaints;
        textSize = AndroidUtilities.dp(15);
        textColor = 0xff000000;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockEmbed) {
        currentMap = embedTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockSlideshow) {
        currentMap = slideshowTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff838c96;
    } else if (parentBlock instanceof TLRPC.TL_pageBlockEmbedPost) {
        if (richText != null) {
            currentMap = embedPostTextPaints;
            textSize = AndroidUtilities.dp(14);
            textColor = 0xff000000;
        }
    } else if (parentBlock instanceof TLRPC.TL_pageBlockVideo) {
        currentMap = videoTextPaints;
        textSize = AndroidUtilities.dp(14);
        textColor = 0xff000000;
    }
    if (currentMap == null) {
        if (errorTextPaint == null) {
            errorTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            errorTextPaint.setColor(0xffff0000);
        }
        errorTextPaint.setTextSize(AndroidUtilities.dp(14));
        return errorTextPaint;
    }
    TextPaint paint = currentMap.get(flags);
    if (paint == null) {
        paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        if ((flags & TEXT_FLAG_MONO) != 0) {
            paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmono.ttf"));
        } else {
            if (parentBlock instanceof TLRPC.TL_pageBlockTitle
                    || parentBlock instanceof TLRPC.TL_pageBlockHeader
                    || parentBlock instanceof TLRPC.TL_pageBlockSubtitle
                    || parentBlock instanceof TLRPC.TL_pageBlockSubheader) {
                if ((flags & TEXT_FLAG_MEDIUM) != 0 && (flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.BOLD_ITALIC));
                } else if ((flags & TEXT_FLAG_MEDIUM) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.BOLD));
                } else if ((flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(Typeface.create("serif", Typeface.ITALIC));
                } else {
                    paint.setTypeface(Typeface.create("serif", Typeface.NORMAL));
                }
            } else {
                if ((flags & TEXT_FLAG_MEDIUM) != 0 && (flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmediumitalic.ttf"));
                } else if ((flags & TEXT_FLAG_MEDIUM) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
                } else if ((flags & TEXT_FLAG_ITALIC) != 0) {
                    paint.setTypeface(AndroidUtilities.getTypeface("fonts/ritalic.ttf"));
                }
            }
        }
        if ((flags & TEXT_FLAG_STRIKE) != 0) {
            paint.setFlags(paint.getFlags() | TextPaint.STRIKE_THRU_TEXT_FLAG);
        }
        if ((flags & TEXT_FLAG_UNDERLINE) != 0) {
            paint.setFlags(paint.getFlags() | TextPaint.UNDERLINE_TEXT_FLAG);
        }
        if ((flags & TEXT_FLAG_URL) != 0) {
            textColor = 0xff4d83b3;
        }
        paint.setColor(textColor);
        currentMap.put(flags, paint);
    }
    paint.setTextSize(textSize);
    return paint;
}