我正在用Libgdx创建一个Android动态壁纸,它只是屏幕上的一个圆圈,它具有随机的颜色和位置,并缓慢向上移动。
问题是设置动态壁纸后,当我再次单击应用程序图标时,而不是显示具有新颜色和位置的新圆圈,而是显示已经运行的圆圈。
拥有新圈子的唯一方法是通过使用其他主题来停止动态壁纸,然后再次启动该应用程序。
我要拥有的是,每次单击图标时,都会加载一个新的动态壁纸,而不会影响已经运行的壁纸,如果我单击“设置为动态壁纸”按钮,则旧的将替换为新的。
Android启动器类
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new CoreLWP(), config);
}
}
动态壁纸课程
public class LiveWallpaper extends AndroidLiveWallpaperService {
public CoreLWP wallpaper;
@Override
public void onCreateApplication () {
super.onCreateApplication();
final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
this.wallpaper = new CoreLWP();
initialize(this.wallpaper, config);
}
}
核心动态壁纸课程
public class CoreLWP extends ApplicationAdapter implements ApplicationListener, AndroidWallpaperListener {
private ShapeRenderer ball;
private float x,y;
private Random random;
private float r,g,b;
@Override
public void create () {
ball = new ShapeRenderer();
random = new Random();
//generating random position
x = random.nextInt(Gdx.graphics.getWidth());
y = random.nextInt(Gdx.graphics.getHeight());
//generating random rgb values
r = getFloatRandomNum(0, 1) ;
g = getFloatRandomNum(0, 1) ;
b = getFloatRandomNum(0, 1) ;
}
@Override
public void render () {
//setting the background
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//updating x and y position
x = x + 0.5f;
y = y + 0.6f;
ball.begin(ShapeRenderer.ShapeType.Filled);
ball.setColor(r,g,b,1);
ball.circle(x,y,100);
ball.end();
}
//returns a random float number between a range
private float getFloatRandomNum(float min, float max) {
float random = min + new Random().nextFloat() * (max - min);
return random;
}
@Override
public void dispose () {
ball.dispose();
}
@Override
public void offsetChange(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {}
@Override
public void previewStateChange(boolean isPreview) {}
@Override
public void iconDropped(int x, int y) {}
}