在动态壁纸上移动圈子

时间:2012-03-19 11:17:16

标签: android live-wallpaper

我必须在动态壁纸中画一个圆圈,当它接触到边界时,绘图的方向会反转(类似于锯齿形格式)。

问题是我能够以这种格式绘制圆圈。但是:

  1. 如何删除先前绘制的圆圈,以便一次只能看到一个圆圈(点)。
  2. 当我重绘位图时,它开始闪烁为什么会发生这种情况?
  3. 代码如下:

    绘制圆圈的线程:

    {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 个答案:

答案 0 :(得分: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);
        }
    }


}