libgdx实现淡入淡出过渡
libgdx实现淡入淡出过渡,环境jdk17+
、libgdx 1.12.0
2023年11月1日11:02:50最新
依赖
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gdx.version>1.12.0</gdx.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-backend-lwjgl3 -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-backend-lwjgl3</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype</artifactId>
<version>${gdx.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.badlogicgames.gdx/gdx-freetype-platform -->
<dependency>
<groupId>com.badlogicgames.gdx</groupId>
<artifactId>gdx-freetype-platform</artifactId>
<version>${gdx.version}</version>
<classifier>natives-desktop</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<!-- 编译后保持方法形参名称不变 -->
<!--<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>-->
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>tencent</id>
<name>tencent</name>
<layout>default</layout>
<url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>nexus</id>
<name>Nexus</name>
<layout>default</layout>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>aliyunmaven</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
实现
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.ScreenUtils;
import org.junit.Test;
/**
* @author lingkang
* created by 2023/11/1
*/
public class TestFade extends ApplicationAdapter {
@Test
public void test() {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.setForegroundFPS(30);
config.setTitle("yzcy");
config.setWindowedMode(800, 600);
new Lwjgl3Application(this, config);
}
Stage stage;
Image img;
Texture texture;
long inTime = 0l;
@Override
public void create() {
stage = new Stage();
texture = new Texture(Gdx.files.internal("badlogic.jpg"));
img = new Image(texture);
img.setSize(texture.getWidth(), texture.getHeight());
img.setOrigin(img.getWidth() / 2, img.getHeight() / 2);
stage.addActor(img);
/**
// 顺序淡入淡出
SequenceAction alpha = Actions.sequence(
Actions.fadeOut(0f),
Actions.delay(2),
Actions.fadeIn(1f),
Actions.delay(1),
Actions.fadeOut(1f)
);
img.addAction(alpha);
*/
float dur = 0.7f;
// Actions.moveBy 移动 Actions.scaleBy 大小 Actions.rotate 旋转
// 淡入
AlphaAction fadeIn = Actions.fadeIn(dur);
img.addAction(fadeIn);
// 点击图片,淡出
AlphaAction fadeOut = Actions.fadeOut(dur);
img.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
img.clearActions();
img.addAction(fadeOut);
return true;
}
});
Gdx.input.setInputProcessor(stage);
}
@Override
public void render() {
ScreenUtils.clear(Color.WHITE);
stage.act();
stage.draw();
}
@Override
public void dispose() {
stage.dispose();
texture.dispose();
}
}