动圈上的动态壁纸 [英] Moving Circle on Live Wallpaper

查看:161
本文介绍了动圈上的动态壁纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绘制动态壁纸了一圈,当它接触绘画的方向被扭转的边界(如在曲折格式的东西)。

问题是我能够画出圆圈这种格式。但是:


  1. 如何删除previously绘制的圆,因此只有单一的圆(点)是一次明显的。

  2. 当我重新绘制位图它开始闪烁,为什么出现这种情况?

code是如下:

主题绘制圆:

  {animRunnable =新的Runnable(){
                公共无效的run(){                    如果(!isRightEndReached&放大器;&安培; moveCircleX< 320){
                        moveCircleX ++;
                        moveCircleY ++;                    }否则如果(isRightEndReached){
                        moveCircleX--;
                        moveCircleY ++;                    }                    如果(moveCircleX> = 320){
                        isRightEndReached = TRUE;                    }如果别人(moveCircleX< = 0){
                        isRightEndReached = FALSE;
                    }                    moveCircle(moveCircleX,moveCircleY);                    如果(moveCircleY == 480){
                        屏幕-re-初始化x和y点//年底移动圆。
                        moveCircleX = intialStartX-10;
                        moveCircleY = intialStartY + 1;
                        isRightEndReached = FALSE;                        //显示壁纸。
                        showWallpaper();                        moveCircle(moveCircleX,moveCircleY);                    }                }
            };
    / **
         *方法动圈
         *
         * @参数点¯x
         * @参数ÿ
         * /
        私人无效moveCircle(INT X,int y)对{            Log.d(X ==+ X,Y ==+ Y);            涂料粉刷=新的油漆();
            SurfaceHolder surfaceHolder = getSurfaceHolder();
            帆布帆布= NULL;
            尝试{
                画布= surfaceHolder.lockCanvas();
                如果(帆布!= NULL){
                    canvas.save();
                    paint.setColor(Color.RED);
                    canvas.drawCircle(X,Y,5,油漆);                    canvas.restore();                }
            }赶上(例外五){
                e.printStackTrace();
            }            最后{
                如果(帆布!= NULL){
                    surfaceHolder.unlockCanvasAndPost(画布);
                }
            }
            animHandler.removeCallbacks(animRunnable);
            如果(ISVISIBLE()){
                animHandler.postDelayed(animRunnable,1000L / 500L);
            }
        }
//显示壁纸的方法。/ **
         *方法来显示壁纸。
         * /
        无效showWallpaper(){
            SurfaceHolder surfaceHolder = getSurfaceHolder();
            帆布帆布= NULL;
            尝试{
                画布= surfaceHolder.lockCanvas();                如果(帆布!= NULL){                    System.out的
                            .println(绘制于秀壁纸方法的位图。);
                    canvas.save();                    BitmapFactory.Options选项=新BitmapFactory.Options();
                    options.inPurgeable = TRUE;
                    位= BitmapFactory.de codeResource(getResources()
                            R.drawable.aquarium,期权);                    canvas.drawColor(0xff000000);                    canvas.drawBitmap(位图,0,0,NULL);
                    canvas.restore();                }
            } {最后
                如果(帆布!= NULL){
                    surfaceHolder.unlockCanvasAndPost(画布);
                }
            }        }}


解决方案

解决:最后我得到通过不集中移除圈,但与新的点一次又一次绘制位图的解决方案。该方法如下:

  {
BitmapFactory.Options选项=新BitmapFactory.Options();
                options.inPurgeable = TRUE;
                位= BitmapFactory.de codeResource(getResources()
                        R.drawable.aquarium,期权);
涂料粉刷=新的油漆();/ **
     *方法移动一圈即绘制新的圆位置的位图。
     *
     * @参数点¯x
     * @参数ÿ
     * /
    私人无效renderBackground(INT X,int y)对{        Log.d(X ==+ X,Y ==+ Y);
         surfaceHolder = getSurfaceHolder();
        帆布帆布= NULL;
        尝试{
            画布= surfaceHolder.lockCanvas();            如果(帆布!= NULL){
                paint.setColor(Color.RED);                canvas.save();                //设置回地面
                canvas.drawBitmap(位图,0,0,NULL);                //写画的圆。
                 paint.setAntiAlias​​(真);
                 canvas.drawCircle(X,Y,15,漆);                canvas.restore();                bitmap.recycle();            }
        }赶上(例外五){
            e.printStackTrace();
        }        最后{
            如果(帆布!= NULL){
                surfaceHolder.unlockCanvasAndPost(画布);
                // showWallpaper();
            }
        }
        animHandler.removeCallbacks(animRunnable);
        如果(ISVISIBLE()){
            animHandler.postDelayed(animRunnable,1000L / 25L);
        }
    }
}

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

        }

}

解决方案

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


}

这篇关于动圈上的动态壁纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆