Java Code Examples for android.graphics.Paint#setTypeface()

The following examples show how to use android.graphics.Paint#setTypeface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleMonthView.java    From SublimePicker with Apache License 2.0 6 votes vote down vote up
/**
 * Applies the specified text appearance resource to a paint, returning the
 * text color if one is set in the text appearance.
 *
 * @param p     the paint to modify
 * @param resId the resource ID of the text appearance
 * @return the text color, if available
 */
private ColorStateList applyTextAppearance(Paint p, int resId) {
    // Workaround for inaccessible R.styleable.TextAppearance_*
    TextView tv = new TextView(mContext);
    if (SUtils.isApi_23_OrHigher()) {
        tv.setTextAppearance(resId);
    } else {
        //noinspection deprecation
        tv.setTextAppearance(mContext, resId);
    }

    p.setTypeface(tv.getTypeface());
    p.setTextSize(tv.getTextSize());

    final ColorStateList textColor = tv.getTextColors();

    if (textColor != null) {
        final int enabledColor = textColor.getColorForState(ENABLED_STATE_SET, 0);
        p.setColor(enabledColor);
    }

    return textColor;
}
 
Example 2
Source File: OC_Reading.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public static float WidthOfText(String txt,Map attrs,float spaceExtraSpace)
{
    OBFont font = (OBFont) attrs.get("font");
    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(font.size);
    textPaint.setTypeface(font.typeFace);
    float lspacing = OBUtils.coalesce((Float)attrs.get(""),0f);
    if (lspacing > 0)
        textPaint.setLetterSpacing(lspacing / font.size);
    float textWidth = textPaint.measureText(txt);

    if(spaceExtraSpace != 0.0)
    {
        int noSpaces = txt.split(".").length - 1;
        if (noSpaces > 0)
            textWidth +=(noSpaces * spaceExtraSpace);
    }
    return textWidth;
}
 
Example 3
Source File: CodeBlockSpan.java    From Markdown with MIT License 6 votes vote down vote up
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
    float size = paint.getTextSize();
    paint.setTextSize(size * TEXT_SIZE_SCALE);
    paint.setTypeface(Typeface.MONOSPACE);

    if (fm != null && lines == null) {
        lines = new ArrayList<>();
        for (CharSequence c : mLines) {
            lines.addAll(measureTextLine(c, 0, c.length(), paint));
        }
    }

    paint.setTextSize(size);
    return mWidth;
}
 
Example 4
Source File: MainKeyboardView.java    From LokiBoard-Android-Keylogger with Apache License 2.0 6 votes vote down vote up
private void drawLanguageOnSpacebar(final Key key, final Canvas canvas, final Paint paint) {
    final Keyboard keyboard = getKeyboard();
    if (keyboard == null) {
        return;
    }
    final int width = key.getWidth();
    final int height = key.getHeight();
    paint.setTextAlign(Align.CENTER);
    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(mLanguageOnSpacebarTextSize);
    final String language = layoutLanguageOnSpacebar(paint, keyboard.mId.mSubtype, width);
    // Draw language text with shadow
    final float descent = paint.descent();
    final float textHeight = -paint.ascent() + descent;
    final float baseline = height / 2 + textHeight / 2;
    paint.setColor(mLanguageOnSpacebarTextColor);
    paint.setAlpha(mLanguageOnSpacebarAnimAlpha);
    canvas.drawText(language, width / 2, baseline - descent, paint);
    paint.clearShadowLayer();
    paint.setTextScaleX(1.0f);
}
 
Example 5
Source File: ResourceUtils.java    From SwipeableRV with Apache License 2.0 6 votes vote down vote up
/**
 * Create a bitmap from a text
 *
 * @param text
 *         Text which the bitmap is created for
 * @param textSize
 *         Text size
 * @param textColor
 *         Text color
 * @param typeface
 *         Typeface of text
 *
 * @return a bitmap on which is text is drawn
 */
public static Bitmap createBitmapFromText(String text, float textSize, int textColor,
                                          Typeface typeface) {
    Paint paint = new Paint(ANTI_ALIAS_FLAG);
    paint.setTextSize(textSize);
    paint.setColor(textColor);
    paint.setTypeface(typeface);
    paint.setTextAlign(Paint.Align.LEFT);
    float baseline = -paint.ascent();
    int width = (int) (paint.measureText(text) + 0.5f);
    int height = (int) (baseline + paint.descent() + 0.5f);

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, 0, baseline, paint);
    return bitmap;
}
 
Example 6
Source File: PowerProgressBar.java    From FileManager with Apache License 2.0 6 votes vote down vote up
private void initPaint() {

        mRoundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mRoundFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCenterTopTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCenterTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCenterBottomTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mRoundPaint.setStyle(Paint.Style.STROKE);
        mRoundFillPaint.setStyle(Paint.Style.STROKE);

        mCenterTopTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mCenterTopTextPaint.setColor(Color.WHITE);
        mCenterTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mCenterTextPaint.setColor(Color.WHITE);
        mCenterBottomTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mCenterBottomTextPaint.setColor(Color.WHITE);
    }
 
Example 7
Source File: TypefaceSpan.java    From memoir with Apache License 2.0 5 votes vote down vote up
private void applyCustomTypeFace(Paint paint, Typeface tf) {
    Typeface old = paint.getTypeface();
    int oldStyle = old == null ? 0 : old.getStyle();

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
 
Example 8
Source File: DrawableUtils.java    From Xndroid with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a rounded square of a certain color with
 * a character imprinted in white on it.
 *
 * @param character the character to write on the image.
 * @param width     the width of the final image.
 * @param height    the height of the final image.
 * @param color     the background color of the rounded square.
 * @return a valid bitmap of a rounded square with a character on it.
 */
@NonNull
public static Bitmap getRoundedLetterImage(@NonNull Character character, int width, int height, int color) {
    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    Paint paint = new Paint();
    paint.setColor(color);
    Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    paint.setTypeface(boldText);
    paint.setTextSize(Utils.dpToPx(14));
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int radius = Utils.dpToPx(2);

    RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
    canvas.drawRoundRect(outer, radius, radius, paint);

    int xPos = (canvas.getWidth() / 2);
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    paint.setColor(Color.WHITE);
    canvas.drawText(character.toString(), xPos, yPos, paint);

    return image;
}
 
Example 9
Source File: IconData.java    From Status with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the estimated width (px) of the icon, or -1
 * if the icon needs to know the available space
 * first.
 *
 * @param height    the height (px) to scale the icon to
 * @param available the available width for the icon, or -1 if not yet calculated
 * @return the estimated width (px) of the icon
 */
public int getWidth(int height, int available) {
    if ((!hasIcon() || iconSize.nextVal() == 0) && (!hasText() || textSize.nextVal() == 0))
        return 0;

    int width = 0;
    if ((hasIcon() && bitmap != null) || (hasText() && text != null))
        width += padding.nextVal();

    if (hasIcon() && bitmap != null) {
        width += iconSize.nextVal();
        width += padding.nextVal();
    }

    if (hasText() && text != null) {
        Paint textPaint = new Paint();
        textPaint.setTextSize(textSize.nextVal());
        textPaint.setTypeface(typeface);

        Rect bounds = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), bounds);
        width += hasText() ? bounds.width() : 0;
        width += padding.nextVal();
    }

    return width;
}
 
Example 10
Source File: RadarWatchFace.java    From radar-watch-face with MIT License 5 votes vote down vote up
private void initializeHourTextPaint() {
    Resources resources = RadarWatchFace.this.getResources();
    Typeface radarTextTypeface = Typeface.createFromAsset(getAssets(), "fonts/NexaLight.ttf");
    float hourTextSize = 20;

    hourTextPaint = new Paint();
    hourTextPaint.setColor(ContextCompat.getColor(RadarWatchFace.this, R.color.tick_color));
    hourTextPaint.setStrokeWidth(resources.getDimension(R.dimen.radar_hand_stroke));
    hourTextPaint.setAntiAlias(true);
    hourTextPaint.setTextAlign(Paint.Align.LEFT);
    hourTextPaint.setTextSize(hourTextSize);
    hourTextPaint.setTypeface(radarTextTypeface);
}
 
Example 11
Source File: MapsActivity.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
private Bitmap writeTextOnDrawable(int drawableId, String text) 
{
	Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

	Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);

	Paint paint = new Paint();
	paint.setStyle(Style.FILL);
	paint.setColor(Color.WHITE);
	paint.setTypeface(tf);
	paint.setTextAlign(Align.CENTER);
	paint.setTextSize(convertToPixels(getBaseContext(), 14));

	Rect textRect = new Rect();
	paint.getTextBounds(text, 0, text.length(), textRect);

	Canvas canvas = new Canvas(bm);

	//If the text is bigger than the canvas , reduce the font size
	if(textRect.width() >= (canvas.getWidth() - 4))     //the padding on either sides is considered as 4, so as to appropriately fit in the text
		paint.setTextSize(convertToPixels(getBaseContext(), 7));        //Scaling needs to be used for different dpi's

	//Calculate the positions
	int xPos = (canvas.getWidth() / 2) - 2;     //-2 is for regulating the x position offset

	//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
	int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2) - 10) ;  

	canvas.drawText(text, xPos, yPos, paint);

	return  bm;
}
 
Example 12
Source File: TextDrawable.java    From ImageLetterIcon with Apache License 2.0 5 votes vote down vote up
private TextDrawable(Builder builder) {
    super(builder.shape);

    // shape properties
    shape = builder.shape;
    height = builder.height;
    width = builder.width;
    radius = builder.radius;

    // text and color
    text = builder.toUpperCase ? builder.text.toUpperCase() : builder.text;
    color = builder.color;
    borderColor = builder.borderColor;

    // text paint settings
    fontSize = builder.fontSize;
    textPaint = new Paint();
    textPaint.setColor(builder.textColor);
    textPaint.setAntiAlias(true);
    textPaint.setFakeBoldText(builder.isBold);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(builder.font);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setStrokeWidth(builder.borderThickness);

    // border paint settings
    borderThickness = builder.borderThickness;
    borderPaint = new Paint();
    borderPaint.setColor(borderColor);
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(borderThickness);

    // drawable paint color
    Paint paint = getPaint();
    paint.setColor(color);
}
 
Example 13
Source File: StyleSpan.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
private static void apply(Paint paint, int style) {
    int oldStyle;

    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int want = oldStyle | style;

    Typeface tf;
    if (old == null) {
        tf = Typeface.defaultFromStyle(want);
    } else {
        tf = Typeface.create(old, want);
    }

    int fake = want & ~tf.getStyle();

    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
 
Example 14
Source File: CalligraphyTypefaceSpan.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void apply(final Paint paint) {
    final Typeface oldTypeface = paint.getTypeface();
    final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0;
    final int fakeStyle = oldStyle & ~typeface.getStyle();

    if ((fakeStyle & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fakeStyle & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(typeface);
}
 
Example 15
Source File: WatchFaceGenerator.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
private Bitmap drawNoDataBitmap() {
    Bitmap resultBitmap = mainWatchfaceImage.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(resultBitmap);

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(textColor);
    paint.setAntiAlias(true);
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    paint.setTextSize(noDataTextSize);
    Rect bounds = new Rect();
    String noDataText = "No Data";
    paint.getTextBounds(noDataText, 0, noDataText.length(), bounds);
    float x = width / 2f - bounds.width() / 2f - bounds.left;

    canvas.drawText(noDataText, x, height - 100, paint); //draw bgValueText
    //draw timestamp
    int timeStampTextPosY = height - 37;//px
    String timeStampText = "at " + hourMinuteString(JoH.tsl());
    paint.setTextSize(timeStampTextSize);
    paint.getTextBounds(timeStampText, 0, timeStampText.length(), bounds);
    canvas.drawText(timeStampText, width - bounds.width(), timeStampTextPosY, paint);

    return resultBitmap;
}
 
Example 16
Source File: IndexBar.java    From ContactsList with Apache License 2.0 5 votes vote down vote up
/******************
 * common
 ******************/

private void initSetting(Context context, AttributeSet attrs) {
    mOnTouchingLetterChangeListener = getDummyListener();
    mNavigators = new ArrayList<>(0);
    mFocusIndex = -1;

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndexBar);
    float textSize = typedArray.getDimension(R.styleable.IndexBar_letterSize, 8);
    int letterColor = typedArray.getColor(R.styleable.IndexBar_letterColor,
            ContextCompat.getColor(getContext(), android.R.color.white));
    mLetterSpacingExtra = typedArray.getFloat(R.styleable.IndexBar_letterSpacingExtra, 1.4f);
    int focusLetterColor = typedArray.getColor(R.styleable.IndexBar_focusLetterColor,
            ContextCompat.getColor(getContext(), android.R.color.white));
    typedArray.recycle();

    mPaint = new Paint();
    mPaint.setTypeface(Typeface.DEFAULT_BOLD);
    mPaint.setAntiAlias(true);
    mPaint.setColor(letterColor);
    mPaint.setTextSize(textSize);

    mFocusPaint = new Paint();
    mFocusPaint.setTypeface(Typeface.DEFAULT_BOLD);
    mFocusPaint.setAntiAlias(true);
    mFocusPaint.setFakeBoldText(true);
    mFocusPaint.setTextSize(textSize);
    mFocusPaint.setColor(focusLetterColor);

}
 
Example 17
Source File: StyleSpan.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void apply(Paint paint, int style) {
    int oldStyle;

    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int want = oldStyle | style;

    Typeface tf;
    if (old == null) {
        tf = Typeface.defaultFromStyle(want);
    } else {
        tf = Typeface.create(old, want);
    }

    int fake = want & ~tf.getStyle();

    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}
 
Example 18
Source File: textImg.java    From Android-Music-Player with MIT License 5 votes vote down vote up
public void removeEfects() {
	hasEffect = false;
	P0 = new Paint();
	P0.setAntiAlias(true);
	P0.setColor(color);
	P0.setTextSize(Size);
	P0.setTypeface(Ui.cd.cuprumFont);
}
 
Example 19
Source File: RadialTimePickerView.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Draw the 12 text values at the positions specified by the textGrid parameters.
 */
private void drawTextElements(Canvas canvas, float textSize, Typeface typeface,
                              ColorStateList textColor, String[] texts, float[] textX, float[] textY, Paint paint,
                              int alpha, boolean showActivated, int activatedDegrees, boolean activatedOnly) {
    paint.setTextSize(textSize);
    paint.setTypeface(typeface);

    // The activated index can touch a range of elements.
    final float activatedIndex = activatedDegrees / (360.0f / NUM_POSITIONS);
    final int activatedFloor = (int) activatedIndex;
    final int activatedCeil = ((int) Math.ceil(activatedIndex)) % NUM_POSITIONS;

    for (int i = 0; i < 12; i++) {
        final boolean activated = (activatedFloor == i || activatedCeil == i);
        if (activatedOnly && !activated) {
            continue;
        }

        final int stateMask = SUtils.STATE_ENABLED
                | (showActivated && activated ? SUtils.STATE_ACTIVATED : 0);
        final int color = textColor.getColorForState(SUtils.resolveStateSet(stateMask), 0);
        paint.setColor(color);
        paint.setAlpha(getMultipliedAlpha(color, alpha));

        canvas.drawText(texts[i], textX[i], textY[i], paint);
    }
}
 
Example 20
Source File: DrawableUtils.java    From JumpGo with Mozilla Public License 2.0 4 votes vote down vote up
@NonNull
public static Bitmap getRoundedNumberImage(int number, int width, int height, int color, int thickness) {
    String text;

    if (number > 99) {
        text = "\u221E";
    } else {
        text = String.valueOf(number);
    }

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    Paint paint = new Paint();
    paint.setColor(color);
    Typeface boldText = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    paint.setTypeface(boldText);
    paint.setTextSize(Utils.dpToPx(14));
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int radius = Utils.dpToPx(2);

    RectF outer = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
    canvas.drawRoundRect(outer, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    radius--;
    RectF inner = new RectF(thickness, thickness, canvas.getWidth() - thickness, canvas.getHeight() - thickness);
    canvas.drawRoundRect(inner, radius, radius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));

    int xPos = (canvas.getWidth() / 2);
    int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(String.valueOf(text), xPos, yPos, paint);

    return image;
}