Font.SourceIdentifier 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳來源字型資料的唯一 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()。
本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。