gpt4 book ai didi

java - 如何使用自定义 TypefaceSpan 类更改字体大小

转载 作者:搜寻专家 更新时间:2023-11-01 08:01:24 25 4
gpt4 key购买 nike

我正在使用这篇文章中 twaddington 的建议:

How to Set a Custom Font in the ActionBar Title?

它工作正常,但我需要更改字体大小。我该怎么做呢?我尝试从

修改参数 int maxSize
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);

是从 TypefaceSpan 类完成还是在 onCreate 方法中完成?

字体跨度

public class TypefaceSpan extends MetricAffectingSpan {
/** An <code>LruCache</code> for previously loaded typefaces. */
private static LruCache<String, Typeface> sTypefaceCache =
new LruCache<String, Typeface>(12);

private Typeface mTypeface;

/**
* Load the {@link Typeface} and apply to a {@link Spannable}.
*/
public TypefaceSpan(Context context, String typefaceName) {
mTypeface = sTypefaceCache.get(typefaceName);

if (mTypeface == null) {
mTypeface = Typeface.createFromAsset(context.getApplicationContext()
.getAssets(), String.format("fonts/%s", typefaceName));

// Cache the loaded Typeface
sTypefaceCache.put(typefaceName, mTypeface);
}
}

@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}

@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
}

MainActivity onCreate

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SpannableString s = new SpannableString("TEST TITLE");
s.setSpan(new TypefaceSpan(this, "MotorwerkOblique.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

ActionBar actionBar = getActionBar();
actionBar.setTitle(s);
}

最佳答案

您将它添加到绘画中,每次更新时您都在应用一个新的绘画,它应该指定文本大小。把字体想象成一种字体/一组字符。 Android 文档指出:

The Typeface class specifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally Paint settings like textSize, textSkewX, textScaleX to specify how text appears when drawn (and measured).

阅读本文后,我建议您完成以下工作。 (不确定这是否会满足您的要求。但请告诉我)

@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
p.setTextSize(25);//or what ever size you want
}

@Override
public void updateDrawState(TextPaint tp) {
tp.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
tp.setTextSize(25);//or what ever size you want
}

关于java - 如何使用自定义 TypefaceSpan 类更改字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20859695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com