libGDX使用一张照片实现按钮点击效果

1 篇文章 0 订阅

照片

照片的大小是230 x 50 一会截取的时候注意一下
在这里插入图片描述

代码

废话不多说,上代码

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.StretchViewport;

/**
 * @ProjectName : wfmImmortalityGame
 * @Author : wu fu man
 * @Time : 2023年9月1日 0001 下午 05:57:22
 * @Description : 描述
 */

public class MainGame extends ApplicationAdapter {

    private static final String TAG = MainGame.class.getSimpleName();

    // 视口世界的宽高统使用 480 * 800, 并统一使用伸展视口(StretchViewport)
    public static final float WORLD_WIDTH = 480;
    public static final float WORLD_HEIGHT = 800;

    // 舞台
    private Stage stage;
    /**
     * 按钮照片
     */
    private FileHandle buttonImg;
    // 按钮
    private Button button;

    @Override
    public void create() {
        // 设置日志输出级别
        Gdx.app.setLogLevel(Application.LOG_DEBUG);

        // 使用伸展视口(StretchViewport)创建舞台
        stage = new Stage(new StretchViewport(WORLD_WIDTH, WORLD_HEIGHT));

        // 将输入处理设置到舞台(必须设置, 否则点击按钮没效果)
        Gdx.input.setInputProcessor(stage);

        /*
         * 读取照片
         */
        buttonImg = Gdx.files.internal("com/wfm/button/1.png");

        /*
         * 第 2 步: 创建 ButtonStyle
         */
        Button.ButtonStyle style = new Button.ButtonStyle();
        /**
         * 截取照片
         * x = 0 y = 0 为按下时显示的照片
         * x = 115 y = 0 为不点击显示的照片
         */
        style.down = new TextureRegionDrawable(new TextureRegion(new Texture(buttonImg), 0, 0, 115, 50));
        style.up = new TextureRegionDrawable(new TextureRegion(new Texture(buttonImg), 115, 0, 115, 50));

        /*
         * 第 3 步: 创建 Button
         */
        button = new Button(style);

        // 设置按钮的位置
        button.setPosition(100, 200);

        // 给按钮添加点击监听器
        button.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.log(TAG, "按钮被点击了");
            }
        });

        /*
         * 第 4 步: 添加 button 到舞台
         */
        stage.addActor(button);
    }

    @Override
    public void render() {
        // 黑色清屏
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // 更新舞台逻辑
        stage.act();
        // 绘制舞台
        stage.draw();
    }

    @Override
    public void dispose() {
        if (stage != null) {
            stage.dispose();
        }
    }

}


效果

未点击

在这里插入图片描述

点击

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值