0

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

编码 :

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)

...

这是更多的错误,但我放了一些

4

1 回答 1

0

由于 Surface 可能已损坏,您可能会遇到问题。所以覆盖surfaceDestroyed这样的:

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

有关更多详细信息,请参阅这篇文章和这篇文章。

希望能帮助到你。

于 2014-10-06T05:09:21.353 回答