gpt4 book ai didi

java - 自定义字体不起作用

转载 作者:行者123 更新时间:2023-12-02 07:13:14 26 4
gpt4 key购买 nike

我目前正在制作一个基本动画教程。在我尝试在代码中包含自定义字体之前,该代码可以正常工作。我想知道是否有人能帮助我。下面我已经包含了 LogCat 和我的字体代码。

Typeface font;

public MyBringBack(Context context) {
super(context);

gPlus = BitmapFactory.decodeResource(getResources(), R.drawable.plusselected);
changingY = 0;
font = Typeface.createFromAsset(context.getAssets(), "G-Unit");

}


@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);

Paint textPaint = new Paint();
textPaint.setARGB(50,254,10,500);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(50);
textPaint.setTypeface(font);
canvas.drawText("mybringback",canvas.getWidth()/2, 200, textPaint);

LogCat:-

03-06 12:31:25.395: E/AndroidRuntime(19933): FATAL EXCEPTION: main
03-06 12:31:25.395: E/AndroidRuntime(19933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learn.tam/com.example.learn.tam.GFX}: java.lang.RuntimeException: native typeface cannot be made
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.os.Looper.loop(Looper.java:137)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-06 12:31:25.395: E/AndroidRuntime(19933): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 12:31:25.395: E/AndroidRuntime(19933): at java.lang.reflect.Method.invoke(Method.java:511)
03-06 12:31:25.395: E/AndroidRuntime(19933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-06 12:31:25.395: E/AndroidRuntime(19933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-06 12:31:25.395: E/AndroidRuntime(19933): at dalvik.system.NativeStart.main(Native Method)
03-06 12:31:25.395: E/AndroidRuntime(19933): Caused by: java.lang.RuntimeException: native typeface cannot be made
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.graphics.Typeface.<init>(Typeface.java:247)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.graphics.Typeface.createFromAsset(Typeface.java:221)
03-06 12:31:25.395: E/AndroidRuntime(19933): at com.example.learn.tam.MyBringBack.<init>(MyBringBack.java:25)
03-06 12:31:25.395: E/AndroidRuntime(19933): at com.example.learn.tam.GFX.onCreate(GFX.java:14)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.Activity.performCreate(Activity.java:5206)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-06 12:31:25.395: E/AndroidRuntime(19933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-06 12:31:25.395: E/AndroidRuntime(19933): ... 11 more

最佳答案

使用

font = Typeface.createFromAsset(context.getAssets(), "G-Unit.ttf");

相反

font = Typeface.createFromAsset(context.getAssets(), "G-Unit");

您需要将带有扩展名的字体文件名(如 ttf、otf...)作为第二个参数传递给 Typeface.createFromAsset 方法

关于java - 自定义字体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15247437/

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