[LIBGDX学习]LibGDX代码详解(一)bitmap font

本文档详细解析了LibGDX库中BitmapFont的使用,包括如何创建、设置颜色、使用标记、调整字体大小以及多页字体。通过示例代码展示了如何在Label和SpriteBatch中应用BitmapFont,以及进行文本换行和截断处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.mygdx.game.utils.GdxTest;

public class BitmapFontTest extends GdxTest {
    private Stage stage;
    private SpriteBatch spriteBatch;
    private BitmapFont font;
    private ShapeRenderer renderer;
    private BitmapFont multiPageFont;
    private GlyphLayout layout;
    private Label label;

    @Override
    public void create () {
        spriteBatch = new SpriteBatch();
        font = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), false);
        // font = new BitmapFont(Gdx.files.internal("data/arial-32-pad.fnt"), false);
        // font = new FreeTypeFontGenerator(Gdx.files.internal("data/arial.ttf")).generateFont(new FreeTypeFontParameter());
        font.getData().markupEnabled = true;
        font.getData().breakChars = new char[] {'-'};

        multiPageFont = new BitmapFont(Gdx.files.internal("data/multipagefont.fnt"));

        // Add user defined color
        Colors.put("PERU", Color.valueOf("CD853F"));
        Colors.put("USER1", Color.valueOf("F5E40A"));
        Colors.put("USER2", Color.valueOf("DC6839"));
        Colors.put("U
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值