动态壁纸+位图+画布

时间:2011-03-22 22:36:28

标签: android android-canvas

大家好!我有这个动态壁纸巫婆我试图通过画布显示位图(可绘制的资源是400k 1900x1200 jpg)。它强制关闭,我不明白为什么(这是我的第一个Android项目)。这些是“重要的”:

    wallEngine() { // CONSTRUCTOR   <<<<<<<<<<<<<<<<<<<
                            mStartTime = SystemClock.elapsedRealtime();


                            Log.v(Logcat, "Contructor ok");

                        }
                //====================================== INIT
                        private final Runnable mDrawWall = new Runnable() { //=========== RUN
                            public void run() {
                                drawFrame();
                            }
                        };

   void drawFrame() {

                           final SurfaceHolder holder = getSurfaceHolder();                                         
                           Canvas c = null;
                           try {
                                c = holder.lockCanvas();
                                if (c != null) {
                                    // draw something
                                    updatePhysics();
                                    doDraw(c);
                                }
                            } finally {
                                if (c != null) holder.unlockCanvasAndPost(c);
                            }

                            // Reschedule the next redraw
                            mHandler.removeCallbacks(mDrawWall);
                            if (mVisible) {
                                mHandler.postDelayed(mDrawWall, 5000);
                            }
                        }


    private void doDraw(Canvas canvas) {                                                                                    
                      //backgroundImage.prepareToDraw();                                                    
                    canvas.drawBitmap(this.backgroundImage, 0, 0, null);                                                                    
                        }


public void onCreate(SurfaceHolder surfaceHolder) {

                            super.onCreate(surfaceHolder);                      surfaceHolder.setFormat(android.graphics.PixelFormat.RGBA_8888);        

                            BitmapFactory.Options options = new BitmapFactory.Options();
                            options.inPurgeable = true;

                            this.backgroundImage = BitmapFactory.decodeResource(getResources(), 
                                com.misca.livewallpapers.parang.R.drawable.a, options);

                            //backgroundImage.prepareToDraw();
                            //setTouchEventsEnabled(true);
                            Log.v(Logcat, "onCreate ok");
                        }

当我尝试在手机上运行时,logcat错误是:

ERROR/AndroidRuntime(7722): FATAL EXCEPTION: main
ERROR/AndroidRuntime(7722): java.lang.NullPointerException
ERROR/AndroidRuntime(7722):     at android.graphics.Canvas.throwIfRecycled(Canvas.java:957)
ERROR/AndroidRuntime(7722):     at android.graphics.Canvas.drawBitmap(Canvas.java:983)
ERROR/AndroidRuntime(7722):     at com.misca.livewallpapers.parang.liveWallpaper$wallEngine.doDraw(liveWallpaper.java:115)
ERROR/AndroidRuntime(7722):     at com.misca.livewallpapers.parang.liveWallpaper$wallEngine.drawFrame(liveWallpaper.java:75)
ERROR/AndroidRuntime(7722):     at com.misca.livewallpapers.parang.liveWallpaper$wallEngine.onSurfaceChanged(liveWallpaper.java:157)
ERROR/AndroidRuntime(7722):     at android.service.wallpaper.WallpaperService$Engine.updateSurface(WallpaperService.java:590)
ERROR/AndroidRuntime(7722):     at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:654)
ERROR/AndroidRuntime(7722):     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:878)
ERROR/AndroidRuntime(7722):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
ERROR/AndroidRuntime(7722):     at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(7722):     at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(7722):     at android.app.ActivityThread.main(ActivityThread.java:3848)
ERROR/AndroidRuntime(7722):     at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(7722):     at java.lang.reflect.Method.invoke(Method.java:507)
ERROR/AndroidRuntime(7722):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
ERROR/AndroidRuntime(7722):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-23 00:26:42.309: ERROR/AndroidRuntime(7722):     at dalvik.system.NativeStart.main(Native Method)

谢谢! :)

======================= 新闻:

                        private void doDraw(Canvas canvas) {                                                                               
                        //backgroundImage.prepareToDraw();                         
                        if(this.backgroundImage!=null)
                        canvas.drawBitmap(this.backgroundImage, 0, 0, null);
                        else
                        Log.v(Logcat, "null");

                    }

似乎位图为空。现在我正在使用480x800以确保没有内存问题。使用if语句(空黑屏)

,应用程序不再崩溃

1 个答案:

答案 0 :(得分:2)

您是否尝试过缩放位图以适应画布?在onSurfaceChanged中执行此操作,以便您知道要使用的宽度和高度。 您应该检查您的位图是否正确解码。你的资源是400K并不重要 - 这是一个非常大的位图,试图加载到内存中。你只有16兆的堆......那个位图大约是9兆(4字节/像素ARGB_8888)。乔治