类androidx.annotation.FontRes源码实例Demo

下面列出了怎么用androidx.annotation.FontRes的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: CommonUtils   文件: FontsManager.java
@NonNull
public static Typeface get(@NonNull Context context, @FontRes int res) {
    if (instance == null) instance = new FontsManager();

    Typeface typeface = instance.cache.get(res);
    if (typeface == null) {
        typeface = ResourcesCompat.getFont(context, res);
        if (typeface == null) throw new IllegalArgumentException("Font doesn't exist!");
        instance.cache.put(res, typeface);
    }

    return typeface;
}
 
源代码2 项目: Carbon   文件: ResourcesCompat.java
/**
 * Used by TintTypedArray.
 *
 * @hide
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public static Typeface getFont(@NonNull Context context, @FontRes int id, TypedValue value,
                               int style, int weight, @Nullable androidx.core.content.res.ResourcesCompat.FontCallback fontCallback) throws NotFoundException {
    if (context.isRestricted()) {
        return null;
    }
    return loadFont(context, id, value, style, weight, fontCallback, null /* handler */,
            true /* isXmlRequest */);
}
 
源代码3 项目: CommonUtils   文件: FontsManager.java
public static void set(@FontRes int res, @NonNull TextView... views) {
    for (TextView view : views) view.setTypeface(get(view.getContext(), res));
}
 
源代码4 项目: CommonUtils   文件: FontsManager.java
public static void set(@NonNull Context context, @NonNull Paint paint, @FontRes int res) {
    paint.setTypeface(get(context, res));
}
 
源代码5 项目: CommonUtils   文件: SuperTextView.java
public void setTypeface(@FontRes int res) {
    FontsManager.set(res, this);
}
 
源代码6 项目: CommonUtils   文件: SuperTextView.java
public Builder typeface(@FontRes int res) {
    view.setTypeface(res);
    return this;
}
 
源代码7 项目: Carbon   文件: ResourcesCompat.java
/**
 * Returns a font Typeface associated with a particular resource ID.
 * <p>
 * This method will block the calling thread to retrieve the requested font, including if it
 * is from a font provider. If you wish to not have this behavior, use
 * {@link #getFont(Context, int, FontCallback, Handler)} instead.
 * <p>
 * Prior to API level 23, font resources with more than one font in a family will only load the
 * font closest to a regular weight typeface.
 *
 * @param context A context to retrieve the Resources from.
 * @param id      The desired resource identifier of a {@link Typeface},
 *                as generated by the aapt tool. This integer encodes the
 *                package, type, and resource entry. The value 0 is an invalid
 *                identifier.
 * @return A font Typeface object.
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 * @see #getFont(Context, int, FontCallback, Handler)
 */
@Nullable
public static Typeface getFont(@NonNull Context context, @FontRes int id)
        throws NotFoundException {
    if (context.isRestricted()) {
        return null;
    }
    return loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, null /* callback */,
            null /* handler */, false /* isXmlRequest */);
}
 
源代码8 项目: Carbon   文件: ResourcesCompat.java
/**
 * Returns a font Typeface associated with a particular resource ID asynchronously.
 * <p>
 * Prior to API level 23, font resources with more than one font in a family will only load the
 * font closest to a regular weight typeface.
 * </p>
 *
 * @param context      A context to retrieve the Resources from.
 * @param id           The desired resource identifier of a {@link Typeface}, as generated by the aapt
 *                     tool. This integer encodes the package, type, and resource entry. The value 0 is an
 *                     invalid identifier.
 * @param fontCallback A callback to receive async fetching of this font. The callback will be
 *                     triggered on the UI thread.
 * @param handler      A handler for the thread the callback should be called on. If null, the
 *                     callback will be called on the UI thread.
 * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
 */
public static void getFont(@NonNull Context context, @FontRes int id,
                           @NonNull androidx.core.content.res.ResourcesCompat.FontCallback fontCallback, @Nullable Handler handler)
        throws NotFoundException {
    Preconditions.checkNotNull(fontCallback);
    if (context.isRestricted()) {
        fontCallback.callbackFailAsync(
                FontRequestCallback.FAIL_REASON_SECURITY_VIOLATION, handler);
        return;
    }
    loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, fontCallback, handler,
            false /* isXmlRequest */);
}