Fuentes TrueType en libGDX
¿Alguien sabe cómo puedo usar una fuente TTF en libGDX? He mirado alrededor y he visto cosas sobre StbTrueTypeFont pero no parece estar en la última versión.
EDIT: Encontré el tipo de fuente StbTrueType, el archivo jar se encuentra en el directorio de extensiones. Lo he añadido a mi proyecto. Ahora sólo tengo que averiguar cómo usarlo. ¿Algún ejemplo?
- ¿Cómo cambiar entre el modo vertical y el modo horizontal con libGDX?
- El deslizador siempre tiene el ancho predeterminado
- Gradle se cuelga en tareas de ejecución: [: android: generateDebugSources
- Manipulación de imágenes JPEG progresivas en Libgdx
- Libgdx o playn?
- ¿Cómo funcionará touchDragged en libgdx?
- Manejo de colisión en Bullet a través de los archivos .g3db
- Cómo utilizar las cámaras LibGDX con Box2D Debug Renderers
- Dibujo de un gradiente en Libgdx
- Libgdx particleEffect de rotación
- Libgdx se bloquea en Android
- La UI de configuración de Libgdx da 2 errores inesperados en el nuevo proyecto GWT
- Problema de Android Studio gradle 2.1.3
Sí, definitivamente necesitarás agregar los gdx-stb-truetype
a tu proyecto como lo has dicho en tu edición. Aquí está cómo usted lo utilizará, bastante straighforward …
Primero debes declarar tu BitmapFont
y los caracteres que BitmapFont
…
BitmapFont font; public static final String FONT_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789][_!$%#@|\\/?-+=()*&.;,{}\"´`'<>";
Entonces necesitas crear la fuente …
font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("font.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); font.setColor(1f, 0f, 0f, 1f);
Puedes jugar con los argumentos que pasas a createBitmapFont()
y verás lo que hacen.
Entonces para hacer la fuente que usted lo haría como lo hace normalmente …
batch.begin(); font.draw(font, "This is some text", 10, 10); batch.end();
Utilice la extensión gdx-freetype:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); BitmapFont font15 = generator.generateData(15); BitmapFont font22 = generator.generateData(22); generator.dispose();
"Para usar gdx-freetype, coge los últimos nightlies, enlaza gdx-freetype.jar
y gdx-freetype-natives.jar
a tu proyecto de escritorio, gdx-freetype.jar
a tu proyecto de Android y copia el armeabi/libgdx-freetype.so
armeabi-v7a/libgdx-freetype.so
y armeabi-v7a/libgdx-freetype.so
en la libs/
carpeta de su proyecto Android, al igual que con los archivos libgdx.so
".
Hizo mucha investigación y encontró este método de trabajo:
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("myfont.ttf")); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = 12; // font size BitmapFont font12 = generator.generateFont(parameter); generator.dispose(); // avoid memory leaks, important
Use esto si ha fallado las respuestas anteriores, libGDX Freetype Wiki para referencia.
Esto está trabajando en un teléfono clon S4: Pinewood es una fuente descargada, en la carpeta de activos. Consulte la estructura de carpetas a continuación.
import android.util.Log; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; public class Game implements ApplicationListener { private SpriteBatch mSpriteBatch; private Texture textureBackground; private BitmapFont mBitmapFont; public void create() { Gdx.graphics.setContinuousRendering(false); Gdx.graphics.requestRendering(); mSpriteBatch = new SpriteBatch(); Texture.setEnforcePotImages(false); textureBackground = new Texture(Gdx.files.internal("background.jpg")); FileHandle fontFile = Gdx.files.internal("Pinewood.ttf"); FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile); mBitmapFont = generator.generateFont(150); generator.dispose(); mBitmapFont.setColor(0.9f, 0.5f, 0.5f, 1); Log.e("Game", "mBitmapFont.getScaleX() : "+mBitmapFont.getScaleX() + ", mBitmapFont.getScaleY() "+mBitmapFont.getScaleY()); } public void render() { Log.e("Game", "render()"); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen. mSpriteBatch.begin(); // Drawing goes here! mSpriteBatch.draw(textureBackground, 0, 0); mBitmapFont.draw(mSpriteBatch, "FPS:"+Gdx.graphics.getFramesPerSecond(), 110, 260); mSpriteBatch.end(); } public void resize(int width, int height) { } public void pause() { } public void resume() { } public void dispose() { } }
- ¿Cómo se puede ejecutar servicio indefinidamente y también permitir la vinculación en android?
- Cómo deshabilitar los efectos de sonido predeterminados para toda mi aplicación o actividad