语言

Font.SourceIdentifier 属性

定义

返回源字体数据的唯一 ID。

public int SourceIdentifier { [Android.Runtime.Register("getSourceIdentifier", "()I", "", ApiSince=31)] get; }
[<get: Android.Runtime.Register("getSourceIdentifier", "()I", "", ApiSince=31)>]
member this.SourceIdentifier : int

属性值

字体源数据的唯一标识符。

属性

注解

返回源字体数据的唯一 ID。

可以使用此标识符作为缓存的键,或者检查是否可以使用字体变体设置内插两个字体。

<code>
              // Following three Fonts, fontA, fontB, fontC have the same identifier.
              Font fontA = new Font.Builder("/path/to/font").build();
              Font fontB = new Font.Builder(fontA).setTtcIndex(1).build();
              Font fontC = new Font.Builder(fontB).setFontVariationSettings("'wght' 700).build();

              // Following fontD has the different identifier from above three.
              Font fontD = new Font.Builder("/path/to/another/font").build();

              // Following fontE has different identifier from above four even the font path is the same.
              // To get the same identifier, please create new Font instance from existing fonts.
              Font fontE = new Font.Builder("/path/to/font").build();
</code>

下面是缓存字体对象的一个示例

<code>
              private LongSparseArray<SparseArray<Font>> mCache = new LongSparseArray<>();

              private Font getFontWeightVariation(Font font, int weight) {
                  // Different collection index is treated as different font.
                  long key = ((long) font.getSourceIdentifier()) << 32 | (long) font.getTtcIndex();

                  SparseArray<Font> weightCache = mCache.get(key);
                  if (weightCache == null) {
                     weightCache = new SparseArray<>();
                     mCache.put(key, weightCache);
                  }

                  Font cachedFont = weightCache.get(weight);
                  if (cachedFont != null) {
                    return cachedFont;
                  }

                  Font newFont = new Font.Builder(cachedFont)
                      .setFontVariationSettings("'wght' " + weight);
                      .build();

                  weightCache.put(weight, newFont);
                  return newFont;
              }
</code>

Java文档android.graphics.fonts.Font.getSourceIdentifier()。

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

适用于