我遵循下一个教程(它的第一部分),但类结构稍微简单
http://www.learnopengles.com/how-to-use-opengl-es-2-in-an-android-live-wallpaper/
当我尝试选择壁纸时,我得到了下一个事件链(预览)
onCreateEngine()->onCreate() -> onResume()->onPause()->onResume()->onPause()
为什么我要onResume()->onPause()两次?
当我点击“设置壁纸”时,我得到额外的onPause()->onResume()并且动态壁纸崩溃(可能是因为 onPause() 调用了 2 次)
此外,我在预览或设置壁纸时看不到像onSurfaceCreated()、onDrawFrame()这样的 OGLES2Renderer 调用。
我错过了什么?
Android清单(其中一部分)
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service
android:name="com.aristarhys.lw.LW"
android:label="@string/app_name"
android:permission="android.permission.BIND_WALLPAPER" >
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/lw" />
res/xml/lw.xml
<?xml version="1.0" encoding="UTF-8"?>
<wallpaper
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/ic_launcher"
android:description="@string/app_desc"/>
LW.java
public class LW extends WallpaperService
{
public class OGLES2Engine extends Engine
{
private class OGLES2SV extends GLSurfaceView
{
public OGLES2SV(Context context)
{
super(context);
}
public void onDestroy()
{
super.onDetachedFromWindow();
}
}
private OGLES2SV SV = null;
@Override
public void onCreate(SurfaceHolder surfaceHolder)
{
super.onCreate(surfaceHolder);
SV = new OGLES2SV(LW.this);
SV.setEGLContextClientVersion(2);
SV.setRenderer(new OGLES2Renderer());
}
@Override
public void onVisibilityChanged(boolean visible)
{
if (!visible)
SV.onResume();
else
SV.onPause();
super.onVisibilityChanged(visible);
}
@Override
public void onDestroy()
{
SV.onDestroy();
super.onDestroy();
}
}
@Override
public Engine onCreateEngine()
{
return new OGLES2Engine();
}
}
OGLES2Renderer.java
public class OGLES2Renderer implements GLSurfaceView.Renderer
{
private static final String LOG_TAG = "GL2Surface";
@Override
public void onDrawFrame(GL10 unused)
{
Log.i(LOG_TAG, "Draw frame");
}
@Override
public void onSurfaceChanged(GL10 unused, int width, int height)
{
Log.i(LOG_TAG, "Surface changed");
}
@Override
public void onSurfaceCreated(GL10 unused_, EGLConfig _unused)
{
Log.i(LOG_TAG, "Surface created");
}
}