2012-03-19 20 views
1

境界線に触れるときにライブ壁紙に円を描く必要があります。図面の向きが逆転します(ジグザグ形式のようなもの)。ライブ壁紙を移動するライブ壁紙

問題は、この形式で円を描くことができることです。しかし:

  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); 
     } 
    } 


}