【问题标题】:How to load resources or assets in GLWallpaper service如何在 GLWallpaper 服务中加载资源或资产
【发布时间】:2012-10-19 06:36:22
【问题描述】:

我正在尝试开发 Android 动态壁纸使用 OpenGL 壁纸服务,我可以创建动态壁纸,就像 Mark F Guerra 的 example 一样,但我想添加一些精灵动画到我的壁纸。

我已经在另一个项目中创建了OpenGL ES sprite 动画。我只想在动态壁纸项目中重新创建我的动画。

但在我的动态壁纸项目中,我无法获取 Context 并从资产或资源中加载我的图像

任何建议或示例代码或链接关于在使用glwallpaper服务时加载资源或资产文件将非常有帮助。

欢迎所有建议和/或示例代码。

【问题讨论】:

    标签: android opengl-es live-wallpaper


    【解决方案1】:

    我们可以使用如下所示的上下文..

    in wallpaper service class: 
    ------------------- 
    renderer = new GlRenderer(this); 
    
    in renderer class: 
    ---------------- 
    private Context context; 
    
    public GlRenderer(Context context) { 
    this.context = context; 
    

    我们可以使用getAssets()getResources() 作为渲染器的参数,而不是this
    使用getAssets(),您可以获得保存在资产文件夹中的文件,使用getResources(),您可以获得放置在项目的资源文件夹中的文件。

    【讨论】:

      【解决方案2】:

      将上下文从引擎传递到渲染器。然后,这里有一些示例代码来加载资产。即 resourceID 是您的 R.drawable.xxx 位图。我在我制作的纹理图集类中有这个,所以有些东西可能没有完全包含在方法中。例如,我可能用来加载位图的选项将包括 inscaled = false,但任何适合您的选项。例如,我还修改了它以删除我的错误处理。

      /**
       * Load the resource and push it to the gpu memory, setup default values
       * @param gl
       * @param context
       * @param resourceID
       * @return glTextureID
       * 
       */
      public int loadFromContext(GL10 gl, Context context, int resourceID) {
          mResourceID = resourceID;
          Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resourceID, sBitmapOptions);
          sourceWidth = bmp.getWidth();
          sourceHeight = bmp.getHeight();
          gl.glGenTextures(1, mGLTextures, 0);
          mGLTextureID = mGLTextures[0];
      
          // bind and set min and mag scaling to bilinear
          gl.glBindTexture(GL10.GL_TEXTURE_2D, mGLTextureID);
          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
      
          // repeat by default
          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
          gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
      
          // upload bmp to video memory
          GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
      
          // check error
          int error = gl.glGetError();
          if (error != GL10.GL_NO_ERROR) {
              // cleanup
              bmp.recycle();
              bmp = null;
              mLoaded = false;
                  // error handling here
          } else {
      
              // unbind.
              gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
              bmp.recycle();
              bmp = null;
              mLoaded = true;
              mDirty = true;
      
            } 
            return mGLTextureID;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-17
        • 1970-01-01
        • 2022-10-30
        • 2021-06-01
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多