0

我有一个我目前正在做的基本动画教程。在我尝试在代码中包含自定义字体之前,该代码正在运行。我想知道是否有人可以帮助我。下面我已经包含了 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);

日志猫:-

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
4

2 回答 2

2

采用

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

反而

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

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

于 2013-03-06T12:32:24.567 回答
0

在 createFromAsset 中使用自定义字体的扩展名。

于 2013-03-06T12:33:28.890 回答