2014-02-25 178 views
1

我想爲我的遊戲設置自定義字體,但我真的不知道如何。我選擇此刻的字體的方法是使用這種方法設置自定義字體

public void loadFont() { 
    font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD), 50, 
      true, Color.WHITE_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); 
    font.load(); 
} 

其中「Typeface.SANS_SERIF」是字體

如果你知道我怎麼能加載從資產文件夾中的字體,你可以請幫助我?謝謝

* *我使用AndEngine-GLES2-AnchorCenter

回答

0

喜請不要這樣

public void loadFont() { 
      font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.createFromAsset(activity.getAssets(), "fonts/CUSTOMFONTNAME.ttf"), Typeface.BOLD), 50, 
        true, Color.TRANSPARENT_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); 
      font.load();} 

如果有比

TextView text = (TextView) findViewById(R.id.custom_font); 
    Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf"); 
    text.setTypeface(font); 

這旁邊的任何文本視圖訪問我的博客http://upadhyayjiteshandroid.blogspot.in/2012/12/android-customfont.html並以此方式執行此操作

Programaticaaly你可以做如下,您可以爲每個TextView的,按鈕等建立子類,並在構造函數中應用自定義字體:

public class BrandTextView extends TextView { 

     public BrandTextView(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
     } 
    public BrandTextView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 
    public BrandTextView(Context context) { 
      super(context); 
    } 
    public void setTypeface(Typeface tf, int style) { 
      if (style == Typeface.BOLD) { 
       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont_Bold.ttf")); 
      } else { 
       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont.ttf")); 
      } 
     } 
} 

然後,只需使用自定義視圖,而不是標準的人(即BrandTextView而不是TextView)。

<com.your.package.BrandTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="View with custom font"/> 
<com.your.package.BrandTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:text="View with custom font and bold typeface"/> 

利用這些都在你的main.xml中,你可以使用它們

另一種可能的方式可以如下以及

FontHelper.applyFont(context, findViewById(R.id.activity_root), "fonts/YourCustomFont.ttf"); 

where applyFont method acn be written as follows 

public static void applyFont(final Context context, final View root, final String fontName) { 
     try { 
      if (root instanceof ViewGroup) { 
       ViewGroup viewGroup = (ViewGroup) root; 
       for (int i = 0; i < viewGroup.getChildCount(); i++) 
        applyFont(context, viewGroup.getChildAt(i), fontName); 
      } else if (root instanceof TextView) 
       ((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName)); 
     } catch (Exception e) { 
      Log.e(TAG, String.format("Error occured when trying to apply %s font for %s view", fontName, root)); 
      e.printStackTrace(); 
     } 
    } 

請訪問更多更好地瞭解http://vision-apps.blogspot.in/2012/02/android-better-way-to-apply-custom-font.html

+0

部分答案似乎直接從vision-apps.blogspot.com/2012/02/android-better-way-to-apply-custom-font.html複製 - 請在您使用其他人的工作時添加歸屬。 – laalto

+0

肯定拉爾託,我這樣做! –

+0

@laalto @laalto我已添加屬性和鏈接http://upadhyayjiteshandroid.blogspot.in/2012/12/android-customfont.html是我自己的博客以及 –

2

在您的Project中加入您的yourfont.ttfyourfont.otfAssets並加載c ustom字體像

Typeface tf = null; 
tf = Typeface.createFromAsset(context.getAssets(), "Helvetica neue.ttf"); 
TextView text = (TextView) findViewById(R.id.yourtextview); 
tv.setTypeface(face); 
2

我想通了。下面的代碼

public void loadFont() { 
     font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.createFromAsset(activity.getAssets(), "fonts/CUSTOMFONTNAME.ttf"), Typeface.BOLD), 50, 
       true, Color.TRANSPARENT_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); 
     font.load(); 
+0

這是偉大的:)對於問題和答案都+1比 –

0

在Andengine-GLES2可以使用自定義的字體onCreateResources這種方式():

FontFactory.setAssetBasePath("font/"); 
    final ITexture fontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR); 

    this.mFont = FontFactory.createFromAsset(this.getFontManager(), fontTexture, activity.getAssets(), "your_font_name.ttf", 40, true, android.graphics.Color.BLACK);  
    this.mFont.load(); 

您可以使用 「Typeface.SANS_SERIF」 字體是這樣的:

this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL), 32); 
    this.mFont.load();