动态壁纸渲染脚本背景图像在android中

时间:2012-11-30 14:24:49

标签: android live-wallpaper bitmapimage renderscript

我是渲染脚本的新手。我正在浏览动态壁纸应用程序。

现在的问题是我没有得到我的背景图片(动态壁纸中的静态背景)。

我附上以下代码。

提前致谢。

LiveWallpaperView文件

public class LiveWallpaperView extends RSSurfaceView {
private RenderScriptGL mRSGL;
private LiveWallpaperRS mRender;

LiveWallpaper sf;

Context context;

public LiveWallpaperView(Context context) {
    super(context);

    this.context = context;

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    super.surfaceCreated(holder);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    super.surfaceChanged(holder, format, w, h);

    if (mRSGL == null) {
        RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
        mRSGL = createRenderScriptGL(sc);
        mRSGL.setSurface(holder, w, h);

        mRender = new LiveWallpaperRS(w, h);

        mRender.init(mRSGL, getResources(), false);
        mRender.start();

    }
}

@Override
protected void onDetachedFromWindow() {
    if (mRSGL != null) {
        mRSGL = null;
        destroyRenderScriptGL();
    }
}

}

LiveWallpaper.rs

// Built-in header with graphics API's
#include "rs_graphics.rsh"
#include "rs_core.rsh"

rs_mesh livewMesh;

// fragment shader
rs_program_fragment gPFLW;
rs_allocation gBgImage; // Background image(*****************************************)
rs_program_fragment gSingleTextureFragmentProgram; // fragment shader

static void drawBackground() {
if (gBgImage.p != 0) {
    rs_matrix4x4 projection, model;
    rsMatrixLoadOrtho(&projection, -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f);
    rsgProgramVertexLoadProjectionMatrix(&projection);

    rsMatrixLoadIdentity(&model);
    rsgProgramVertexLoadModelMatrix(&model);

    rsgBindTexture(gSingleTextureFragmentProgram, 0, gBgImage);

    rsgDrawQuad(
        gBgVertices[0].x, gBgVertices[0].y, gBgVertices[0].z,
        gBgVertices[1].x, gBgVertices[1].y, gBgVertices[0].z,
        gBgVertices[2].x, gBgVertices[2].y, gBgVertices[0].z,
        gBgVertices[3].x, gBgVertices[3].y, gBgVertices[0].z
    );
  } else {
    //rsgClearColor(gBgColor.x, gBgColor.y, gBgColor.z, gBgColor.w);
  }
}

后台方法在root方法中调用,然后在同一个类中调用。

LiveWallpaperRS.java

    public void setBackgroundBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return;
    }
    final Allocation bitmapAllocation = Allocation.createFromBitmap(mRS, bitmap,   Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_GRAPHICS_TEXTURE);
    mScript.set_gBgImage(bitmapAllocation);
}

最后我在LiveWallpaperService类中调用了它

        @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);


        if (mRenderScriptGL != null) {
            mRenderScriptGL.setSurface(holder, width, height);
        }
        if (mlivewRS == null) {
            mlivewRS = new LiveWallpaperRS(width, height);

            mlivewRS.setBackgroundBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
            mlivewRS.init(mRenderScriptGL, getResources(), isPreview());



            mlivewRS.start();
        } else {
            //mlivewRS.resize(width, height);
        }
    }

1 个答案:

答案 0 :(得分:0)

您应该在mlivewRs.setBackgroundBitmap回调中mlivewRS.init方法之后致电onSurfaceChanged