我正在使用OpenGL ES的Android動態壁紙,我已經從基礎 http://www.learnopengles.com/how-to-use-opengl-es-2-in-an-android-live-wallpaper/Updating(升級)的OpenGL ES的喜好變動2.0紋理
代碼關閉教程,我目前正在努力讓這個切換偏好將替換不同的紋理。我想這樣做是爲了讓所有的紋理在開始時都會被加載,但是有很多紋理,並且這會花費太長時間(我不能像加載遊戲一樣加載屏幕,所以牆紙必須在一秒之內加載意味着< 3.0 MB的紋理)。我已經決定,它很可能是最好使用texSubImage2D方法,好消息來切換紋理的是,我在渲染器類實現這個像這樣:
String imageID = preferences.getString("texture_model", "1");
Log.d("texture change", "changeTexture called with imageID: " + preferences.getString("texture_model", "1"));
Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.girl2crop);
switch(imageID)
{
case "1":
bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.girl2crop);
Log.d("texture change", "texture 1 called");
break;
case "2":
bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.girlstudentcrop);
Log.d("texture change", "texture 2 called");
break;
case "3":
bmp = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.girlbluecrop);
Log.d("texture change", "texture 3 called");
break;
}
// Bind texture to texturename
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[1]);
GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0,0, bmp);
bmp.recycle();
因爲這隻能由被稱爲在GL主線程,我用這段代碼在WallpaperService類調用在渲染類方法:
@Override
public void onVisibilityChanged(boolean visible)
{
super.onVisibilityChanged(visible);
if (visible)
{
glSurfaceView.onResume();
sensorManager.registerListener(this,accelerometer, SensorManager.SENSOR_DELAY_GAME);
Log.d("onResume", "was called on " + glSurfaceView.toString());
} else
{
if(rendererHasBeenSet)
{
glSurfaceView.queueEvent(new Runnable()
{
// This method will be called on the rendering
// thread:
public void run()
{
renderer.refresh();
}
});
glSurfaceView.onPause();
sensorManager.unregisterListener(this);
Log.d("onPause", "was called on " + glSurfaceView.toString());
}
}
}
,我快到的問題是,每次我切換喜好,壁紙確實會改變,但只有在壁紙「刷新」後(通過打開另一個應用程序,或者旋轉主屏幕,才能恢復,暫停和恢復) N)。這些變化不會立即發生,我需要他們去做。有誰知道如何解決這個問題,或者更好的方法來做到這一點?