2013-01-11 146 views
2

我正在嘗試使用我繪製的幾張圖像製作動畫Android背景。我一步一步地繪製圖像,但手機的繪製速度很慢。來自圖像的Android動畫壁紙

我想這一點:

public class MyWallpaperService extends WallpaperService { 
     private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3, 
       R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7, 
       R.drawable.p8, R.drawable.p9, R.drawable.p10, R.drawable.p11, 
       R.drawable.p12, R.drawable.p13, R.drawable.p14, R.drawable.p15, 
       R.drawable.p16, R.drawable.p17, R.drawable.p18, R.drawable.p19, 
       R.drawable.p20, R.drawable.p21, R.drawable.p22, R.drawable.p23, 
       R.drawable.p24 }; 

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

     private class MyWallpaperEngine extends Engine { 
      private final Handler handler = new Handler(); 
      private final Runnable drawRunner = new Runnable() { 
       @Override 
       public void run() { 
        drawImage(images[0]); 
       } 

      }; 

      private Paint paint = new Paint(); 
      private boolean touch = true; 

      @Override 
      public void onVisibilityChanged(boolean visible) { 
       if (visible) { 
        handler.post(drawRunner); 
       } else { 
        handler.removeCallbacks(drawRunner); 
       } 
      } 

      @Override 
      public void onSurfaceDestroyed(SurfaceHolder holder) { 
       super.onSurfaceDestroyed(holder); 
       handler.removeCallbacks(drawRunner); 
      } 

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

      @Override 
      public void onTouchEvent(MotionEvent event) { 
       if (partOne) { 
        if (touch) { 
         touch = false; 
         for (int i = 0; i < images.length; i++) 
          imagePostDelay(i, i * 100, i == images.length - 1 ? true 
            : false); 
        } 
       } 
       super.onTouchEvent(event); 
      } 

      private void drawImage(int id) { 
       WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); 
       Display display = wm.getDefaultDisplay(); 
       @SuppressWarnings("deprecation") 
       int width = display.getWidth(); // deprecated 960 
       @SuppressWarnings("deprecation") 
       int height = display.getHeight(); // deprecated 540 
       final Resources res = getResources(); 
       Bitmap bitmap; 
       Canvas canvas = null; 
       SurfaceHolder holder = getSurfaceHolder(); 
       bitmap = BitmapFactory.decodeResource(res, id); 
       bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); 
       try { 
        canvas = holder.lockCanvas(); 
        canvas.drawBitmap(bitmap, 0, 0, paint); 
       } catch (NullPointerException e) { 
       } finally { 
        if (canvas != null) 
         holder.unlockCanvasAndPost(canvas); 
       } 
      } 

      private void imagePostDelay(final int index, int delay, 
        final boolean allowTouch) { 
       new Handler().postDelayed(new Runnable() { 
        public void run() { 
         try { 
          drawImage(images[index]); 
         } catch (ArrayIndexOutOfBoundsException e) { 
         } 
         if (allowTouch) 
          touch = true; 
        } 
       }, delay); 
      } 
     } 

我試圖將圖像保存在onCreate方法妥善解決,但它並沒有幫助。然後我用較少的圖像嘗試,但它拋出了一個OutOfMemory異常。這可能是由圖像數量造成的。主要的是捕捉圖像並沒有什麼幫助。它仍然很慢。

  • 有什麼好的教程可以幫助在Android上製作實況幻燈片 背景嗎?
  • 在xml中以正確的分辨率存儲圖像並使用setWallpaper()方法更改圖像時是否是個好主意? setWallpaper()方法會更快嗎?

回答

1

要繪製更有效地移動

bitmap = BitmapFactory.decodeResource(res, id); 
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); 

.... 
private class MyWallpaperEngine extends Engine { 
bitmap = BitmapFactory.decodeResource(res, id); 

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

     // size the background bitmap so it draws efficiently 
     bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); 

     drawFrame(); 
    } 

.... 
void drawFrame() { 
Canvas c = null; 
     try { 
      c = holder.lockCanvas(); 
      if (c != null) { 
       // This will draw on every cycle of the Runnable, 
       // so don't do any decoding of Bitmap here, 
       // that would be very inefficient. 
       canvas.drawBitmap(bitmap, 0, 0, paint); 
     } 
     } finally { 
      if (c != null) holder.unlockCanvasAndPost(c); 
     } 

     mHandler.removeCallbacks(drawRunner); 
     if (mVisible) { 
      mHandler.postDelayed(drawRunner, 1000/25); 
     } 
    } 
+0

私有類MyWallpaperEngine延伸引擎{ 位圖= BitmapFactory.decodeResource(RES,ID); 但是,當我解碼所有圖像並存儲它們時,它將拋出OutOfMemoryException。我有24張照片,點擊後我將他們全部畫出來。我需要在繪製之前進行解碼,對於商店來說這很重要。 –