我目前堅持使用opengl ES2製作實時背景。Android動態壁紙:訪問資源文件夾
在我的渲染器中,我想從資源文件夾訪問紋理和着色器,但我無法找到一種方法來做到這一點。
我目前的嘗試只會導致崩潰。
return new SurfaceRenderer((Activity) this.getApplicationContext(), WallpaperService.this);
這是SurfaceRenderer構造函數中的第一個參數!
後來這些變量將被用來訪問這樣的文件:
final int resVertex = activity.getResources().getIdentifier(input+"_vertexshader","raw", activity.getPackageName());
int resID = activity.getResources().getIdentifier(path,"raw", activity.getPackageName());
InputStream inputStream = activity.getResources().openRawResource(resID);
等。
凡活動是在SurfaceRenderer構造函數的第一個參數,和CTX第二個!
對此的任何解決方案?
編輯: 試圖實施解決方案,但我仍然有問題!
這是我的渲染器的構造:
public SurfaceRenderer(GLWallpaperService activity, Context ctx) {
this.activity = activity;
cc = activity;
shaderlib = new ShaderLib(activity, ctx);
}
它是用於活動轉發給我的着色器創建的對象,最終運行此:
inputStream = activity.getContext().getAssets().open(filename);
沒有的getContext功能可用,當我試圖做出一個,我只能結束一個空指針,在這個命令,任何解決方案?
謝謝,當我回家的時候會試試這個! – user1419305
嗨,試圖實施您的解決方案,但我有問題。在OP中解釋他們的解決方案? – user1419305
@ user1419305我添加了更多代碼示例。如您所見,我將上下文傳遞給渲染器的構造函數。然後,您可以通過從牆紙(它具有cusom getContext()方法)或通過評估保存的上下文來使用上下文。希望這可以幫助。 – keaukraine