Condividi tramite


Font.SourceIdentifier Property

Definition

Returns the unique ID of the source font data.

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

Property Value

an unique identifier for the font source data.

Attributes

Remarks

Returns the unique ID of the source font data.

You can use this identifier as a key of the cache or checking if two fonts can be interpolated with font variation settings.

<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>

Here is an example of caching font object that has

<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 documentation for android.graphics.fonts.Font.getSourceIdentifier().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to