1

I have to draw a circle in live wallpaper when it touches the boundary the direction of drawing gets reversed (something like in zigzag format).

The problem is i am able to draw circle in this format. But:

  1. How to remove the previously drawn circle so that only single circle (dot) is visible at a time.
  2. When i redraw the bitmap it starts flickering why this happens?

Code is as follows:

Thread to draw circle:

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

        }

}
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33

1 Answers1

1

SOLVED: finally i got the solution by not concentrating on removing circle but drawing bitmap again and again with new point. The method is as follows:

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


}
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33