我正在尝试创建一个简单的动态壁纸,它可以在主屏幕幻灯片上滑动图像。
所以我写了这段代码。它在某些设备上有效,但在几个设备中出现内存不足错误。
我知道保存几张大图像需要很多内存。
有人有解决这个内存不足问题的想法吗?
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;
}
我需要你的帮助!
等待你的好主意。谢谢你。