-4

我最近用 18 张图片创建了自己的 Android 动态壁纸项目,但它不能设置为壁纸。如果我将其设置为墙纸,我会看到以下错误消息。不幸的是,示例已停止工作。图像分辨率 480x800。我使用了 18 张图片。项目大小为 2.67 MB。但我在我的项目中找不到任何错误。有谁知道这个错误是什么?

public class star extends WallpaperService {

    public void onCreate(){
        super.onCreate();
    }

    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
        return new WallpaperEngine();
    }

    private class WallpaperEngine extends Engine{

        Bitmap i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i21,i22,i23,i24,i25,i26,i27,i28,i29,i30;
        int position;
        int screenwidth;
        DisplayMetrics metric;
        int count=0;

        private final Handler hanlder=new Handler();
        private boolean visible=false;
        private final Runnable r=new Runnable() {

            @Override
            public void run() {
                 draw();
            }
        };

        WallpaperEngine() {
            i1=BitmapFactory.decodeResource(getResources(), R.drawable.image1);
            i2=BitmapFactory.decodeResource(getResources(), R.drawable.image2);
            i3=BitmapFactory.decodeResource(getResources(), R.drawable.image3);
            i4=BitmapFactory.decodeResource(getResources(), R.drawable.image4);
            i5=BitmapFactory.decodeResource(getResources(), R.drawable.image5);
            i6=BitmapFactory.decodeResource(getResources(), R.drawable.image6);
            i7=BitmapFactory.decodeResource(getResources(), R.drawable.image7);
            i8=BitmapFactory.decodeResource(getResources(), R.drawable.image8);
            i9=BitmapFactory.decodeResource(getResources(), R.drawable.image9);
            i10=BitmapFactory.decodeResource(getResources(), R.drawable.image10);
            i11=BitmapFactory.decodeResource(getResources(), R.drawable.image11);
            i12=BitmapFactory.decodeResource(getResources(), R.drawable.image12);
            i13=BitmapFactory.decodeResource(getResources(), R.drawable.image13);
            i14=BitmapFactory.decodeResource(getResources(), R.drawable.image14);
            i15=BitmapFactory.decodeResource(getResources(), R.drawable.image15);
            i16=BitmapFactory.decodeResource(getResources(), R.drawable.image16);
            i17=BitmapFactory.decodeResource(getResources(), R.drawable.image17);
            i18=BitmapFactory.decodeResource(getResources(), R.drawable.image18);
            i19=BitmapFactory.decodeResource(getResources(), R.drawable.image19);
            i20=BitmapFactory.decodeResource(getResources(), R.drawable.image20);   
            position=0;
            metric=getBaseContext().getResources().getDisplayMetrics();
            screenwidth= metric.widthPixels;
        }

        public void onCreate(SurfaceHolder holder){
            super.onCreate(holder);
        }

        public void onVisibilityChanged(boolean visible){
            this.visible=visible;
            if (visible) {
                hanlder.post(r);
            } else {
                hanlder.removeCallbacks(r);
            }
        }

        public void onSurfaceDestroyed(SurfaceHolder holder){
            visible=false;
            hanlder.removeCallbacks(r);
        }

        public void draw(){

            Bitmap arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i19,i18,i17,i16,i15,i14,i13,i12,i11,i10,i9,i8,i7,i6,i5,i4,i3,i2};
            SurfaceHolder holder=getSurfaceHolder();
            Canvas c=null;

            try {

                c=holder.lockCanvas();
                if(c!=null){
                    c.drawColor(Color.WHITE);
                    c.drawBitmap(arr[count], position,0, null);
                    ++count;

                    if(count==arr.length){
                        count=0;
                    }
                }

            } finally {
                if(c!=null){
                    holder.unlockCanvasAndPost(c);
                }
            }

            hanlder.removeCallbacks(r);
            if(visible){
                hanlder.postDelayed(r, 70);
            }

        }
    }
}

在我收到以下 LOGCAT 错误后:

09-26 14:07:01.005: E/AndroidRuntime(2767): FATAL EXCEPTION: main
09-26 14:07:01.005: E/AndroidRuntime(2767): java.lang.OutOfMemoryError
09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.example.stargate.star$WallpaperEngine.<init>(star.java:55)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.example.stargate.star.onCreateEngine(star.java:27)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1034)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.os.Handler.dispatchMessage(Handler.java:99)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.os.Looper.loop(Looper.java:137)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.app.ActivityThread.main(ActivityThread.java:5041)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at java.lang.reflect.Method.invokeNative(Native Method)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at java.lang.reflect.Method.invoke(Method.java:511)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at dalvik.system.NativeStart.main(Native Method)

09-26 14:08:11.935: E/Trace(4999): error opening trace file: No such file or directory (2)
4

2 回答 2

1

你为什么要在一个上面绘制 18 个位图?你的内存不足了!仅在需要时解码和加载它们!!!

将该代码修剪为 3 个位图,看看错误是否仍然存在,我敢打赌它不会。

于 2013-10-04T18:21:11.710 回答
0

您可以使用此代码。

class Diwali extends Engine  {

    private boolean mVisible;

    private final Runnable diwaliImg = new Runnable() {
        public void run() {
            drawFrame();
        }
    };

    int i=0;
    int[] pirates = {
        R.drawable.a1, R.drawable.a2,
        R.drawable.a3, R.drawable.a4,
        R.drawable.a5, R.drawable.a6,
        R.drawable.a7, R.drawable.a8,
        R.drawable.a9, R.drawable.a10,
        R.drawable.a11, R.drawable.a12,
        R.drawable.a13, R.drawable.a14
    };

    @Override
    public void onCreate(SurfaceHolder holder){
        super.onCreate(holder);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(diwaliImg);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        mVisible = visible;
        if (visible) {
            drawFrame();
        } else {
            mHandler.removeCallbacks(diwaliImg);
        }
    }

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

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

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceDestroyed(holder);
        mVisible = false;
        mHandler.removeCallbacks(diwaliImg);
    }


    @Override
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,float yStep, int xPixels, int yPixels) {
        drawFrame();
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
    }

    private void drawFrame() {
        // TODO Auto-generated method stub
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                drawPirate(c);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
        mHandler.removeCallbacks(diwaliImg);
        if (mVisible) {
            mHandler.postDelayed(diwaliImg,60);
        }
    }

    private void drawPirate(Canvas c) {
        // TODO Auto-generated method stub
        Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

        i++;

        if (i == 13) {
            i = 0;
        }
        Matrix matrix = new Matrix();

        c.drawBitmap(icon, matrix, null);
        icon.recycle();
    }
}
于 2013-10-31T11:59:33.010 回答