android.graphics.Paint#setTypeface ( )源码实例Demo

下面列出了android.graphics.Paint#setTypeface ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: SublimePicker   文件: SimpleMonthView.java
/**
 * 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;
}
 
源代码2 项目: GLEXP-Team-onebillion   文件: OC_Reading.java
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;
}
 
源代码3 项目: Markdown   文件: CodeBlockSpan.java
@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;
}
 
源代码4 项目: FileManager   文件: PowerProgressBar.java
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);
    }
 
源代码5 项目: SwipeableRV   文件: ResourceUtils.java
/**
 * 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;
}
 
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);
}
 
源代码7 项目: Xndroid   文件: DrawableUtils.java
/**
 * 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;
}
 
源代码8 项目: SublimePicker   文件: RadialTimePickerView.java
/**
 * 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);
    }
}
 
源代码9 项目: Android-Music-Player   文件: textImg.java
public void removeEfects() {
	hasEffect = false;
	P0 = new Paint();
	P0.setAntiAlias(true);
	P0.setColor(color);
	P0.setTextSize(Size);
	P0.setTypeface(Ui.cd.cuprumFont);
}
 
源代码10 项目: android_9.0.0_r45   文件: StyleSpan.java
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);
}
 
源代码11 项目: ContactsList   文件: IndexBar.java
/******************
 * 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);

}
 
源代码12 项目: xDrip   文件: WatchFaceGenerator.java
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;
}
 
源代码13 项目: UltimateAndroid   文件: CalligraphyTypefaceSpan.java
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);
}
 
源代码14 项目: PowerFileExplorer   文件: StyleSpan.java
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);
}
 
源代码15 项目: ImageLetterIcon   文件: TextDrawable.java
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);
}
 
源代码16 项目: vocefiscal-android   文件: MapsActivity.java
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;
}
 
源代码17 项目: radar-watch-face   文件: RadarWatchFace.java
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);
}
 
源代码18 项目: Status   文件: IconData.java
/**
 * 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;
}
 
源代码19 项目: memoir   文件: TypefaceSpan.java
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);
}
 
源代码20 项目: JumpGo   文件: DrawableUtils.java
@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;
}