2012-03-19 32 views
1

當它接觸到邊界時,圖形的方向會顛倒過來(類似於鋸齒形格式),我必須在動態壁紙中繪製一個圓。在動態壁紙上移動的圓圈

問題是我能夠以這種格式畫圈。但是:

  1. 如何刪除先前繪製的圓,以便一次只能看到單個圓(點)。
  2. 當我重畫位圖時,它開始閃爍,爲什麼會發生這種情況?

代碼如下:

主題繪製圓:

{animRunnable = new Runnable() { 
       public void run() { 

        if (!isRightEndReached && moveCircleX < 320) { 
         moveCircleX++; 
         moveCircleY++; 

        } else if (isRightEndReached) { 
         moveCircleX--; 
         moveCircleY++; 

        } 

        if (moveCircleX >= 320) { 
         isRightEndReached = true; 

        } else if (moveCircleX <= 0) { 
         isRightEndReached = false; 
        } 

        moveCircle(moveCircleX, moveCircleY); 

        if (moveCircleY == 480) { 
         // end of screen -re-init x and y point to move circle. 
         moveCircleX = intialStartX-10; 
         moveCircleY = intialStartY+1; 
         isRightEndReached = false; 

         // show wallpaper. 
         showWallpaper(); 

         moveCircle(moveCircleX, moveCircleY); 

        } 

       } 
      }; 


    /** 
     * Method to move circle 
     * 
     * @param x 
     * @param y 
     */ 
     private void moveCircle(int x, int y) { 

      Log.d("x==" + x, "y==" + y); 

      Paint paint = new Paint(); 
      SurfaceHolder surfaceHolder = getSurfaceHolder(); 
      Canvas canvas = null; 
      try { 
       canvas = surfaceHolder.lockCanvas(); 
       if (canvas != null) { 
        canvas.save(); 
        paint.setColor(Color.RED); 
        canvas.drawCircle(x, y, 5, paint); 

        canvas.restore(); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      finally { 
       if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } 
      animHandler.removeCallbacks(animRunnable); 
      if (isVisible()) { 
       animHandler.postDelayed(animRunnable, 1000L/500L); 
      } 
     } 


//Show wallpaper method. 

/** 
     * Method to show wallpaper. 
     */ 
     void showWallpaper() { 
      SurfaceHolder surfaceHolder = getSurfaceHolder(); 
      Canvas canvas = null; 
      try { 
       canvas = surfaceHolder.lockCanvas(); 

       if (canvas != null) { 

        System.out 
          .println("Drawing bitmap in show Wallpaper method."); 
        canvas.save(); 

        BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inPurgeable = true; 
        bitmap = BitmapFactory.decodeResource(getResources(), 
          R.drawable.aquarium, options); 

        canvas.drawColor(0xff000000); 

        canvas.drawBitmap(bitmap, 0, 0, null); 
        canvas.restore(); 

       } 
      } finally { 
       if (canvas != null) { 
        surfaceHolder.unlockCanvasAndPost(canvas); 
       } 
      } 

     } 

} 

回答

1

解決:最後我得到的不是專注於去除圈,但與新點一遍又一遍繪製位圖的解決方案。方法如下:

{ 
BitmapFactory.Options options = new BitmapFactory.Options(); 
       options.inPurgeable = true; 
       bitmap = BitmapFactory.decodeResource(getResources(), 
         R.drawable.aquarium, options); 
Paint paint = new Paint(); 

/** 
    * Method to move circle i.e to draw bitmap with new circle position. 
    * 
    * @param x 
    * @param y 
    */ 
    private void renderBackground(int x, int y) { 

     Log.d("x==" + x, "y==" + y); 


     surfaceHolder = getSurfaceHolder(); 
     Canvas canvas = null; 
     try { 
      canvas = surfaceHolder.lockCanvas(); 

      if (canvas != null) { 
       paint.setColor(Color.RED); 

       canvas.save(); 

       // set Back ground 


       canvas.drawBitmap(bitmap, 0, 0, null); 

       // write draw circle. 
       paint.setAntiAlias(true); 
       canvas.drawCircle(x, y, 15, paint); 

       canvas.restore(); 

       bitmap.recycle(); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     finally { 
      if (canvas != null) { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
       // showWallpaper(); 
      } 
     } 
     animHandler.removeCallbacks(animRunnable); 
     if (isVisible()) { 
      animHandler.postDelayed(animRunnable, 1000L/25L); 
     } 
    } 


}