下面列出了怎么用androidx.annotation.FontRes的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
/**
* 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 */);
}
public static void set(@FontRes int res, @NonNull TextView... views) {
for (TextView view : views) view.setTypeface(get(view.getContext(), res));
}
public static void set(@NonNull Context context, @NonNull Paint paint, @FontRes int res) {
paint.setTypeface(get(context, res));
}
public void setTypeface(@FontRes int res) {
FontsManager.set(res, this);
}
public Builder typeface(@FontRes int res) {
view.setTypeface(res);
return this;
}
/**
* 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 */);
}
/**
* 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 */);
}