/** * Sets the typeface size used by this view. * * @param typeface the typeface to use on the text. */ public void setTypeface(Typeface typeface) { if (textStyle == Typeface.BOLD_ITALIC) { typeface = Typeface.create(typeface, Typeface.BOLD_ITALIC); } else if (textStyle == Typeface.BOLD) { typeface = Typeface.create(typeface, Typeface.BOLD); } else if (textStyle == Typeface.ITALIC) { typeface = Typeface.create(typeface, Typeface.ITALIC); } textPaint.setTypeface(typeface); onTextPaintMeasurementChanged(); }
/** * @param familyName if start with 'asset:' prefix, then load font from asset folder. * @return */ public static Typeface load(Context context, String familyName, int style) { if(familyName != null && familyName.startsWith(PREFIX_ASSET)) synchronized (sCachedFonts) { try { if (!sCachedFonts.containsKey(familyName)) { final Typeface typeface = Typeface.createFromAsset(context.getAssets(), familyName.substring(PREFIX_ASSET.length())); sCachedFonts.put(familyName, typeface); return typeface; } } catch (Exception e) { return Typeface.DEFAULT; } return sCachedFonts.get(familyName); } return Typeface.create(familyName, style); } }
private static Typeface loadRoboto(Context context, Roboto roboto) { // try to load asset try { Typeface t = Typeface.createFromAsset(context.getAssets(), roboto.getPath()); pathCache.put(roboto.getPath(), t); familyStyleCache[roboto.getTextStyle()].put(roboto.getFontFamily(), t); return t; } catch (Exception e) { } // try system font Typeface t = Typeface.create(roboto.getFontFamily(), roboto.getTextStyle()); if (t != null) { pathCache.put(roboto.getPath(), t); familyStyleCache[roboto.getTextStyle()].put(roboto.getFontFamily(), t); return t; } return null; } }
private Typeface typefaceForStyle(Typeface typeface, String style) { int styleInt = Typeface.NORMAL; boolean containsItalic = style.contains("Italic"); boolean containsBold = style.contains("Bold"); if (containsItalic && containsBold) { styleInt = Typeface.BOLD_ITALIC; } else if (containsItalic) { styleInt = Typeface.ITALIC; } else if (containsBold) { styleInt = Typeface.BOLD; } if (typeface.getStyle() == styleInt) { return typeface; } return Typeface.create(typeface, styleInt); } }
private Builder() { text = ""; color = Color.GRAY; textColor = Color.WHITE; borderThickness = 0; width = -1; height = -1; shape = new RectShape(); font = Typeface.create("sans-serif-light", Typeface.NORMAL); fontSize = -1; isBold = false; toUpperCase = false; }
PeekabooTouchHelperCallback(Context context) { super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT); mDefaultTextColor = ContextCompat.getColor(context, AppUtils.getThemedResId(context, android.R.attr.textColorPrimary)); mPaint.setTextSize(context.getResources().getDimensionPixelSize(R.dimen.text_size_small)); mPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); mPadding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin); }
/** Returns a stub typeface immediately. */ @Implementation public static Typeface getFontSync(FontRequest request) { return Typeface.create(request.getQuery(), Typeface.NORMAL); } }
private static @Nullable Unbinder parseBindFont(Object target, Field field, View source) { BindFont bindFont = field.getAnnotation(BindFont.class); if (bindFont == null) { return null; } validateMember(field); int id = bindFont.value(); int style = bindFont.style(); Context context = source.getContext(); Class<?> fieldType = field.getType(); Object value; if (fieldType == Typeface.class) { Typeface font = ResourcesCompat.getFont(context, id); value = style != Typeface.NORMAL ? Typeface.create(font, style) : font; } else { throw new IllegalStateException(); // TODO } trySet(field, target, value); return Unbinder.EMPTY; }
canvas.drawCircle(x, y + MINI_DAY_NUMBER_TEXT_SIZE - DAY_HIGHLIGHT_CIRCLE_MARGIN, DAY_HIGHLIGHT_CIRCLE_SIZE, mSelectedCirclePaint); mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); } else { mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)); mMonthNumPaint.setColor(mDisabledDayTextColor); } else if (mSelectedDay == day) { mMonthNumPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); mMonthNumPaint.setColor(mSelectedDayTextColor); } else if (mHasToday && mToday == day) {
public void initialize(Context context, int amOrPm) { if (mIsInitialized) { Log.e(TAG, "AmPmCirclesView may only be initialized once."); return; } Resources res = context.getResources(); mUnselectedColor = res.getColor(R.color.white); mSelectedColor = res.getColor(R.color.blue); mAmPmTextColor = res.getColor(R.color.ampm_text_color); mSelectedAlpha = SELECTED_ALPHA; String typefaceFamily = res.getString(R.string.sans_serif); Typeface tf = Typeface.create(typefaceFamily, Typeface.NORMAL); mPaint.setTypeface(tf); mPaint.setAntiAlias(true); mPaint.setTextAlign(Align.CENTER); mCircleRadiusMultiplier = Float.parseFloat(res.getString(R.string.circle_radius_multiplier)); mAmPmCircleRadiusMultiplier = Float.parseFloat(res.getString(R.string.ampm_circle_radius_multiplier)); String[] amPmTexts = new DateFormatSymbols().getAmPmStrings(); mAmText = amPmTexts[0]; mPmText = amPmTexts[1]; setAmOrPm(amOrPm); mAmOrPmPressed = -1; mIsInitialized = true; }
mMonthTitlePaint.setAntiAlias(true); mMonthTitlePaint.setTextSize(MONTH_LABEL_TEXT_SIZE); mMonthTitlePaint.setTypeface(Typeface.create(mMonthTitleTypeface, Typeface.BOLD)); mMonthTitlePaint.setColor(mDayTextColor); mMonthTitlePaint.setTextAlign(Align.CENTER); mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE); mMonthDayLabelPaint.setColor(mMonthDayTextColor); mMonthTitlePaint.setTypeface(Typeface.create(mDayOfWeekTypeface, Typeface.BOLD)); mMonthDayLabelPaint.setStyle(Style.FILL); mMonthDayLabelPaint.setTextAlign(Align.CENTER);
public void initialize(Context context, int amOrPm) { if (mIsInitialized) { Log.e(TAG, "AmPmCirclesView may only be initialized once."); return; } Resources res = context.getResources(); mWhite = res.getColor(R.color.comm_white); mAmPmTextColor = res.getColor(R.color.ampm_text_color); mBlue = Utils.resolveColor(getContext(), R.attr.themeColor, res.getColor(R.color.comm_blue)); String typefaceFamily = res.getString(R.string.sans_serif); Typeface tf = Typeface.create(typefaceFamily, Typeface.NORMAL); mPaint.setTypeface(tf); mPaint.setAntiAlias(true); mPaint.setTextAlign(Align.CENTER); mCircleRadiusMultiplier = Float.parseFloat(res.getString(R.string.circle_radius_multiplier)); mAmPmCircleRadiusMultiplier = Float.parseFloat(res.getString(R.string.ampm_circle_radius_multiplier)); String[] amPmTexts = new DateFormatSymbols().getAmPmStrings(); mAmText = amPmTexts[0]; mPmText = amPmTexts[1]; setAmOrPm(amOrPm); mAmOrPmPressed = -1; mIsInitialized = true; }
GifBadge(Context context) { if (bitmap == null) { final DisplayMetrics dm = context.getResources().getDisplayMetrics(); final float density = dm.density; final float scaledDensity = dm.scaledDensity; final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint .SUBPIXEL_TEXT_FLAG); textPaint.setTypeface(Typeface.create(TYPEFACE, TYPEFACE_STYLE)); textPaint.setTextSize(TEXT_SIZE * scaledDensity); final float padding = PADDING * density; final float cornerRadius = CORNER_RADIUS * density; final Rect textBounds = new Rect(); textPaint.getTextBounds(GIF, 0, GIF.length(), textBounds); height = (int) (padding + textBounds.height() + padding); width = (int) (padding + textBounds.width() + padding); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setHasAlpha(true); final Canvas canvas = new Canvas(bitmap); final Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); backgroundPaint.setColor(BACKGROUND_COLOR); if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){ canvas.drawRoundRect(0, 0, width, height, cornerRadius, cornerRadius, backgroundPaint); } // punch out the word 'GIF', leaving transparency textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); canvas.drawText(GIF, padding, height - padding, textPaint); } paint = new Paint(); }
mMonthTitlePaint.setAntiAlias(true); mMonthTitlePaint.setTextSize(MONTH_LABEL_TEXT_SIZE); mMonthTitlePaint.setTypeface(Typeface.create(mMonthTitleTypeface, Typeface.BOLD)); mMonthTitlePaint.setColor(mDayTextColor); mMonthTitlePaint.setTextAlign(Align.CENTER); mMonthDayLabelPaint.setTextSize(MONTH_DAY_LABEL_TEXT_SIZE); mMonthDayLabelPaint.setColor(mDayTextColor); mMonthDayLabelPaint.setTypeface(Typeface.create(mDayOfWeekTypeface, Typeface.NORMAL)); mMonthDayLabelPaint.setStyle(Style.FILL); mMonthDayLabelPaint.setTextAlign(Align.CENTER);
/** * Constructor for <code>LetterTileProvider</code> * * @param context The {@link Context} to use */ public LetterTileProvider(Context context) { final Resources res = context.getResources(); mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); mPaint.setColor(Color.WHITE); mPaint.setTextAlign(Paint.Align.CENTER); mPaint.setAntiAlias(true); mColors = res.obtainTypedArray(R.array.letter_tile_colors); mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size); //mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.); mDefaultBitmap = drawableToBitmap(ContextCompat.getDrawable(context, R.drawable.ic_person_white_24dp)); mWidth = res.getDimensionPixelSize(R.dimen.letter_tile_size); mHeight = res.getDimensionPixelSize(R.dimen.letter_tile_size); }
public static Typeface getTypeface(Context context, String fontFamily, int textStyle) { // get from cache Typeface t = familyStyleCache[textStyle].get(fontFamily); if (t != null) return t; // Roboto? for (Roboto style : Roboto.values()) { if (style.getFontFamily().equals(fontFamily) && style.getTextStyle() == textStyle) { t = loadRoboto(context, style); if (t != null) return t; } } // load from system res t = Typeface.create(fontFamily, textStyle); if (t != null) { familyStyleCache[textStyle].put(fontFamily, t); return t; } return Typeface.DEFAULT; }
Typeface tf = Typeface.create(typefaceFamily, Typeface.NORMAL); mPaint.setTypeface(tf); mPaint.setAntiAlias(true);
@Test public void create_withFamily_shouldCreateTypeface() { Typeface typeface = Typeface.create(Typeface.create("roboto", Typeface.BOLD), Typeface.ITALIC); assertThat(typeface.getStyle()).isEqualTo(Typeface.ITALIC); assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto"); assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.ITALIC); }
@Test public void create_withFamilyName_shouldCreateTypeface() { Typeface typeface = Typeface.create("roboto", Typeface.BOLD); assertThat(typeface.getStyle()).isEqualTo(Typeface.BOLD); assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo("roboto"); assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.BOLD); }
@Test public void create_withoutFamily_shouldCreateTypeface() { Typeface typeface = Typeface.create((Typeface) null, Typeface.ITALIC); assertThat(typeface.getStyle()).isEqualTo(Typeface.ITALIC); assertThat(shadowOf(typeface).getFontDescription().getFamilyName()).isEqualTo(null); assertThat(shadowOf(typeface).getFontDescription().getStyle()).isEqualTo(Typeface.ITALIC); }