AndEngine字体问题

时间:2014-01-02 07:02:31

标签: android android-layout andengine

我正在学习动力学。 我正在使用此代码显示Hellow单词

        package com.example.textfontexample;
    import org.andengine.engine.camera.Camera;
    import org.andengine.engine.options.EngineOptions;
    import org.andengine.engine.options.ScreenOrientation;
    import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
    import org.andengine.entity.scene.Scene;
    import org.andengine.entity.scene.background.Background;
    import org.andengine.entity.text.Text;
    import org.andengine.opengl.font.Font;
    import org.andengine.opengl.font.FontFactory;
    import org.andengine.opengl.texture.Texture;
    import org.andengine.opengl.texture.TextureOptions;
    import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
    import org.andengine.ui.activity.SimpleBaseGameActivity;
    import org.andengine.util.color.Color;

    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.view.WindowManager;

    public class myactivity extends SimpleBaseGameActivity
    {

     private final int CAMERA_WIDTH = 320;
     private final int CAMERA_HEIGHT = 480;

     private Camera  m_Camera;
     private Scene  m_Scene;

     private Font  font;
     private Text  text;

     @Override
     public EngineOptions onCreateEngineOptions()
     {
      m_Camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
      EngineOptions en = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(
        CAMERA_WIDTH, CAMERA_HEIGHT), m_Camera);

      return en;
     }

     @Override
     protected void onCreateResources()
     {
        //determine the density
     WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
     Display display = windowManager.getDefaultDisplay();
     DisplayMetrics displayMetrics = new DisplayMetrics();
     display.getMetrics(displayMetrics);
     int density = (int)(displayMetrics.density);

     //scale desired size 25 by density
         int fontSize = (int) (25 * density);
      font = FontFactory.createFromAsset(this.getFontManager(), this.getTextureManager(), 1024, 1024, this.getAssets(),
        "times.ttf", fontSize, true, android.graphics.Color.BLACK);
      font.load();



    //Texture fontTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    //font = new Font(fontTexture, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), fontSize, true, Color.WHITE);
    //mEngine.getTextureManager().loadTexture(fontTexture);
    //mEngine.getFontManager().loadFont(font);

     }

     @Override
     protected Scene onCreateScene()
     {
      m_Scene = new Scene();
      m_Scene.setBackground(new Background(Color.WHITE));

      text = new Text(0, 0, font, "Hello Android", this.getVertexBufferObjectManager());
      m_Scene.attachChild(text);

      text.setPosition(CAMERA_WIDTH/2 - (text.getWidth()/2), CAMERA_HEIGHT/2 - (text.getHeight()/2));
      return m_Scene;
     }

    }

它显示的字体如enter image description here

正如你所看到的那样,像素正在变形,字体看起来太丑了。 如何显示哪些像素没有拉伸的正确字体文字???

1 个答案:

答案 0 :(得分:1)

   FontFactory.setAssetBasePath("font/");
        BitmapTextureAtlas fontTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
        mFont = FontFactory.createFromAsset(this.getFontManager(), fontTexture, this.getAssets(), "Droid.ttf", 48, true, android.graphics.Color.WHITE);
    fontTexture.load();
     mFont.load();

尝试此模式。