沿主屏幕转换更改图像

时间:2012-10-09 07:36:38

标签: android live-wallpaper

我正在尝试创建一个简单的livewallpaper,用主屏幕幻灯片滑动图像 所以我写了这段代码。它在一些设备上工作,但我在几个设备中出现Out Of Memory错误。
我知道要保存几张大图像会占用大量内存
有人有任何想法解决这个Out Of Memory问题吗?

void drawFrame() {
    SurfaceHolder holder = getSurfaceHolder();
    Canvas c;
    if(mScreenNo < 1 || isPreview() || offsetOK == false){
        Log.d("drawFrame","mScreenNo < 1 || isPreview()");
        c = holder.lockCanvas();
        c.drawBitmap(bg_I, 0, bgY, p);
    }else{
    float bg_w = bg_I.getWidth();
    Float[] bg_x = new Float[mScreenNo +1];

    for(int i=0; i < bg_x.length; i++){
        bg_x[i] = bgX + bg_w * i;
    }

        c = holder.lockCanvas();

    Log.d("draw"," bgX="+bgX);

    for(int i=0; i < bg_x.length; i++){
        switch(i%3){
        case 0:
        c.drawBitmap(bg_I, bg_x[i], bgY, p);
        break;
        case 1:
        c.drawBitmap(bg_II, bg_x[i], bgY, p);
        break;
            case 2:
        c.drawBitmap(bg_III, bg_x[i], bgY, p);
        break;
            }
    }
    }

    holder.unlockCanvasAndPost(c);
    handler.removeCallbacks(drawThread);
    if (visible)
    handler.postDelayed(drawThread, 25);
    }
    }

在onOffsetChanged,我喜欢这个。

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
                float yStep, int xPixels, int yPixels) {
            mXoffset = xOffset;
            mXStep = xStep;
            mXPixels = xPixels;
            mScreenNo = (int)(1/mXStep);//Number of screens(-1)
            mPageNo = (int)(mXoffset / mXStep);//current screen page(the first page is 0)
            mWallStep = getWallpaperDesiredMinimumWidth()/2 /mScreenNo;
            float mwall_screen = rescale.getScreenWidth() / mWallStep;
            bgX = mXPixels * mwall_screen;
            offsetOK = true;
}

我需要你的帮助! 等待你的好主意。谢谢。

1 个答案:

答案 0 :(得分:0)

我自己刚刚解决了这个问题。 也许这是一种愚蠢的解决方法,但它现在运作良好。

我如何解决问题只是修改for循环如下:

    for(int i=0; i < bg_x.length; i++){
    if(-bg_w < bg_x[i]&& bg_x[i] <= bg_w){
        Log.d("drawFrame","draw this time");
        switch(i%3){
        case 0:
            c.drawBitmap(bg_I, bg_x[i], bgY, p);
            break;
        case 1:
            c.drawBitmap(bg_II, bg_x[i], bgY, p);
            break;
        case 2:
            c.drawBitmap(bg_III, bg_x[i], bgY, p);
            break;
        }
    }else{
        Log.d("drawFrame","no draw this time");
    }
}
希望这会对某人有所帮助。