Example usage for android.text TextPaint setTextSize

List of usage examples for android.text TextPaint setTextSize

Introduction

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

Prototype

public void setTextSize(float textSize) 

Source Link

Document

Set the paint's text size.

Usage

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 w w w .  j  a  v  a 2  s  .co  m
    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.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);/*from  w w  w  . ja  v  a 2 s.  c om*/
    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.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  ww w. j  a va2  s .com*/
    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:com.silentcircle.contacts.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*ww w  .jav  a  2 s.c  o m*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(ContextCompat.getColor(mContext, R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f,
                ContextCompat.getColor(mContext, R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.android.contacts.common.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*  w  w  w.  j a  v  a 2s. c  o m*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.android.contacts.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *//*from www.  j  a v a2 s. com*/
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    final Drawable phoneDrawable = r.getDrawableForDensity(actionResId, mIconDensity);
    // These icons have the same height and width so either is fine for the size.
    final Bitmap phoneIcon = BitmapUtil.drawableToBitmap(phoneDrawable, phoneDrawable.getIntrinsicHeight());

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type if we're pre-O. O created shortcuts have the
    // app badge which overlaps the type overlay.
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);
    if (!BuildCompat.isAtLeastO() && overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(r.getColor(R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f, r.getColor(R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    int iconWidth = icon.getWidth();
    if (BuildCompat.isAtLeastO()) {
        // On O we need to calculate where the phone icon goes slightly differently. The whole
        // canvas area is 108dp, a centered circle with a diameter of 66dp is the "safe zone".
        // So we start the drawing the phone icon at
        // 108dp - 21 dp (distance from right edge of safe zone to the edge of the canvas)
        // - 24 dp (size of the phone icon) on the x axis (left)
        // The y axis is simply 21dp for the distance to the safe zone (top).
        // See go/o-icons-eng for more details and a handy picture.
        final int left = (int) (mIconSize - (45 * density));
        final int top = (int) (21 * density);
        canvas.drawBitmap(phoneIcon, left, top, photoPaint);
    } else {
        dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
        canvas.drawBitmap(phoneIcon, null, dst, photoPaint);
    }

    canvas.setBitmap(null);
    return icon;
}

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);//  ww w  .j  av a  2s. co m

    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: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  va 2s. 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();//from  w w w . ja va 2 s .  co 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);// ww  w  . ja va 2 s  .  c  o  m
    textPaint.setTextSize(FONT11);
    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;
}