2012-06-19 71 views
0

我最近在我的动态壁纸中发布了一个关于屏幕旋转的问题。为了测试我为什么有这些问题,我创建了下面的简单程序:RE:动态壁纸屏幕旋转

package com.live.waller; 

import android.graphics.Canvas; 
import android.graphics.Color; 
import android.os.Handler; 
import android.service.wallpaper.WallpaperService; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 

public class LiveWallpaperService extends WallpaperService { 


/** Called when the activity is first created. */ 

@Override 
public Engine onCreateEngine() { 
    // TODO Auto-generated method stub 
    return new LiveWallerEngine(); 
} 


class LiveWallerEngine extends Engine { 

    SurfaceHolder holder; 
    private Handler mHandle; 

    LiveWallerEngine() { 
     mHandle = new Handler(); 
     holder = getSurfaceHolder(); 
    } 


    private Runnable runner = new Runnable() { 

     public void run() { 
      // TODO Auto-generated method stub 
      drawFrame(); 
     } 

    }; 

    public void drawFrame() { 
     while(isVisible()) { 
      if(!holder.getSurface().isValid()) 
       continue; 

      Canvas c = null; 

      try{ 
       c = holder.lockCanvas(); 

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

    public void drawSurface(Canvas c) { 


     c.save(); 

     c.drawColor(Color.argb(255, 100, 200, 124)); 

     c.restore(); 

    } 

    @Override 
    public void onCreate(SurfaceHolder surfaceHolder) { 
     // TODO Auto-generated method stub 
     super.onCreate(surfaceHolder); 

     setTouchEventsEnabled(true); 
     mHandle.post(runner); 

    } 

    @Override 
    public void onOffsetsChanged(float xOffset, float yOffset, 
      float xOffsetStep, float yOffsetStep, int xPixelOffset, 
      int yPixelOffset) { 
     // TODO Auto-generated method stub 
     super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, 
       xPixelOffset, yPixelOffset); 
    } 

    @Override 
    public void onSurfaceChanged(SurfaceHolder holder, int format, 
      int width, int height) { 
     // TODO Auto-generated method stub 
     super.onSurfaceChanged(holder, format, width, height); 

     mHandle.post(runner); 
    } 

    @Override 
    public void onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     super.onTouchEvent(event); 
    } 

    @Override 
    public void onVisibilityChanged(boolean visible) { 
     // TODO Auto-generated method stub 
     super.onVisibilityChanged(visible); 

    } 

    @Override 
    public void setTouchEventsEnabled(boolean enabled) { 
     // TODO Auto-generated method stub 
     super.setTouchEventsEnabled(enabled); 
    } 
} 

}

当我旋转我的墙纸,我正与一个空白屏幕映入眼帘。有人知道我的代码有什么问题吗?

当我运行了墙纸,我在logcat中得到这些错误:

ActivityManager: force stopping package com.live.waller uid=10046 
PackageManager: Not granting permission android.permission.BIND_WALLPAPER to package com.live.waller (protectionLevel=3 flags=0xbe46) 
dalvikvm: GC_CONCURRENT freed 609K, 43% free 4604K/8071K, external 904K/1222K, paused 6ms+7ms 
dalvikvm: GC_EXPLICIT freed 320K, 44% free 4561K/8071K, external 904K/1222K, paused 138ms 
dalvikvm: GC_EXTERNAL_ALLOC freed 197K, 51% free 2955K/6023K, external 1736K/1742K, paused 78ms 

什么奇怪的是,我在logcat中得到了强行停止的消息,但不是当我把我的壁纸模拟器。

+0

您的while(isVisible())和holder.getSurface()。isValid())如何评估?它是否可见?并且表面有效? – Davos555

+0

是的,当我运行壁纸屏幕是绿色的。只有当我旋转屏幕时它才会变黑。 – Denizen

+0

当您旋转屏幕时,这两个条件是否为真? – Davos555

回答

2

只要isVisible()返回true,drawFrame()方法就会卡在一个无限循环中。当您使用处理程序获得Runnable时,无需使用while语句来循环锁定和张贴新鲜绘制的Canvas。

可以在SDK的Cube Live Wallpaper示例中找到更好的drawFrame()实现。

private void drawFrame() { 
    Canvas c = null; 

    try { 
     // Get a Canvas from the surfaceHolder, so we got something to paint on 
     c = holder.lockCanvas(); 

     // Make sure we got a valid (non-null) canvas. 
     if(c != null) { 
      // Draw something onto the canvas 
      drawSurface(c); 
     } 
    } finally { 
     if(c != null) 
      // Notify the SurfaceHolder that we are done painting the canvas, 
      // and we want it shown on the screen 
      holder.unlockCanvasAndPost(c); 
    } 

    // If your wallpaper is going to have animated objects, you will have to tell 
    // the handler to schedule new runs on your Runnable object. 

    // First we remove any pending task in the Handlers message queue 
    mHandle.removeCallbacks(runner); 

    // Then we tell the Handler to schedule a new run some time in the future. The 
    // time we specify here will decide how often the screen updates, or in other words 
    // the FPS of your wallpaper. If the wallpaper is not visible, there is no reason to update wallpaper. So we only schedule a new run, if mIsVisible is true.  
    if(mIsVisible) { 
     mHandle.postDelayed(runner, 1000/desiredFPS); 
    } 
} 

现在,我们使用Engine.onVisibilityChanged()来决定壁纸是否可见。

@Override 
public void onVisibilityChanged(boolean visible) { 
    // Set mIsVisible equal to visible, so that drawFrame() can decide wheter to reschedule run or not. 
    mIsVisible = visible; 

    if (visible) { 
     // Since drawFrame() tells the handler to schedule new runs, we only need to call drawFrame() once. In drawFrame(), mHandle.postDelayed() will then continuously update the screen, as long as its visible. 
     drawFrame(); 
    } else { 
     // If not, remove any pending posts, since we no longer need to update the wallpaper. 
     mHandle.removeCallbacks(runner); 
    } 
} 

如果我们烤漆表面以某种方式被破坏(例:如果用户设置了新的背景下),我们也希望消除任何未决的职务。因此,在Engine.onSurfaceDestroyed(),我们做

@Override 
public void onSurfaceDestroyed(SurfaceHolder holder) { 
    super.onSurfaceDestroyed(holder); 
    mHandle.removeCallbacks(runner); 
} 

关于力闭合问题: 什么API级别的你去上运行壁纸?

+0

谢谢,屏幕旋转现在工作正常! – Denizen

+0

至于力量关闭,我正在编译到2.3.3模拟器。 – Denizen

+0

你确定你的AndroidManifest设置正确吗?如果不是,发布它将是有用的;) – Ole