设置动态壁纸android时崩溃

时间:2014-10-06 04:29:01

标签: android android-canvas android-wallpaper

我在一张活壁纸上工作; 在这种情况下,当我看到它在预览中,它没有任何问题并显示图像 但是当它设置为设备背景时,它将被崩溃

代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Handler;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.Toast;


public class MoharamLiveWallpaperService extends WallpaperService {

    WallpaperEngine wallpeperEngine = new WallpaperEngine();

    @Override
    public Engine onCreateEngine() {

        return wallpeperEngine;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        wallpeperEngine.Destroy();
    }

    public class WallpaperEngine extends Engine
    {
        final int DELAY_AFTER_FRAME = 16;
        final int SLIDE_NUMBER = 4;
        int screenWidth;
        int screenHeight;
        int backgroundXOffset=0;
        Handler handler = new Handler();
        Bitmap backgroundBitmap;


        Runnable updateRunnable = new Runnable() {

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

        public WallpaperEngine() {
        }

        void Update()
        {

            Draw();
            handler.postDelayed(updateRunnable, DELAY_AFTER_FRAME);
        }

        void Draw()
        {
            try
            {
                SurfaceHolder surfaceHolder = getSurfaceHolder();
                Canvas canvas = surfaceHolder.lockCanvas();
                if(canvas != null)
                {
                    canvas.drawColor(Color.BLACK);
                    canvas.drawBitmap(backgroundBitmap, backgroundXOffset, 0, null);
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }


            }
            catch (Exception e) {

            }
        }

        @Override
        public void onSurfaceCreated(SurfaceHolder holder) {
            super.onSurfaceCreated(holder);
            screenWidth = holder.getSurfaceFrame().right;
            screenHeight = holder.getSurfaceFrame().bottom;
            backgroundBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_image0);
            int bitmapWidth = (backgroundBitmap.getWidth()*screenHeight)/backgroundBitmap.getHeight();

            backgroundBitmap = Bitmap.createScaledBitmap(backgroundBitmap, bitmapWidth, screenHeight, false);
            handler.post(updateRunnable);
        }

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

            screenWidth = width;
            screenHeight = height;
        }

        @Override
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep, int xPixelOffset,
                int yPixelOffset) {
            super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                    xPixelOffset, yPixelOffset);
            Log.i("WP", "xOffset : " +xOffset+" yOffset : "+yOffset+"\n"+
                        "xOffsetStep : "+xOffsetStep+" yOffsetStep : "+yOffsetStep+"\n"+
                        "xPixelOffset : "+xPixelOffset+" yPixelOffset : "+yPixelOffset);
        }

        public void Destroy()
        {
            handler.removeCallbacks(updateRunnable);
        }
    }

}

和错误:

10-06 07:53:10.420: E/BaseSurfaceHolder(2253): Exception locking surface
10-06 07:53:10.420: E/BaseSurfaceHolder(2253): java.lang.IllegalStateException: Surface has already been released.
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.view.Surface.checkNotReleasedLocked(Surface.java:386)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.view.Surface.lockCanvas(Surface.java:235)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:157)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.os.Handler.handleCallback(Handler.java:730)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.os.Looper.loop(Looper.java:137)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invoke(Method.java:525)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 07:53:10.420: E/BaseSurfaceHolder(2253):  at dalvik.system.NativeStart.main(Native Method)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253): Exception locking surface
10-06 07:53:10.440: E/BaseSurfaceHolder(2253): java.lang.IllegalStateException: Surface has already been released.
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.view.Surface.checkNotReleasedLocked(Surface.java:386)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.view.Surface.lockCanvas(Surface.java:235)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:157)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.os.Handler.handleCallback(Handler.java:730)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.os.Looper.loop(Looper.java:137)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invoke(Method.java:525)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 07:53:10.440: E/BaseSurfaceHolder(2253):  at dalvik.system.NativeStart.main(Native Method)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253): Exception locking surface
10-06 07:53:10.550: E/BaseSurfaceHolder(2253): java.lang.IllegalStateException: Surface has already been released.
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.view.Surface.checkNotReleasedLocked(Surface.java:386)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.view.Surface.lockCanvas(Surface.java:235)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:157)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.os.Handler.handleCallback(Handler.java:730)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.os.Looper.loop(Looper.java:137)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invoke(Method.java:525)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-06 07:53:10.550: E/BaseSurfaceHolder(2253):  at dalvik.system.NativeStart.main(Native Method)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253): Exception locking surface
10-06 07:53:10.660: E/BaseSurfaceHolder(2253): java.lang.IllegalStateException: Surface has already been released.
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.view.Surface.checkNotReleasedLocked(Surface.java:386)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.view.Surface.lockCanvas(Surface.java:235)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:184)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:157)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at 
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.os.Handler.handleCallback(Handler.java:730)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.os.Looper.loop(Looper.java:137)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invokeNative(Native Method)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at java.lang.reflect.Method.invoke(Method.java:525)
10-06 07:53:10.660: E/BaseSurfaceHolder(2253):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

...

它的错误更多,但我把它们中的一些

1 个答案:

答案 0 :(得分:0)

由于Surface可能已被销毁,您可能会遇到问题。因此,像这样覆盖surfaceDestroyed

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    synchronized (this) {
        //manage flags
        //remove handler's callbacks
    } 
}

有关详细信息,请参阅this帖子和this one

希望它有所帮助。