简体   繁体   English

动态壁纸具有弹跳效果

[英]Live Wallpaper with bouncing effect

I'm creating a life wallpaper for android which "ic_launcher" is able to bounce when it "hit" the left/right edge of the screen. 我正在为Android创建一个生活壁纸,当“ ic_launcher”“击中”屏幕的左/右边缘时,它可以弹跳。

    public class LiveWallpaperService extends WallpaperService 
    {
            int x,y;

            public void onCreate()
            {
                    super.onCreate();
            }

            public void onDestroy() 
            {
                    super.onDestroy();
            }

            public Engine onCreateEngine() 
            {
                    return new MyWallpaperEngine();
            }

            class MyWallpaperEngine extends Engine 
            {

                    private final Handler handler = new Handler();
                    private final Runnable drawRunner = new Runnable() {
                        @Override
                        public void run() {
                            draw();
                        }
                    };
                    private boolean visible = true;
                    public Bitmap image1,backgroundImage;

                    MyWallpaperEngine() 
                    {

                            image1 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
                            backgroundImage = BitmapFactory.decodeResource(getResources(),R.drawable.pika);
                            x=130; 
                            y=200;  

                    }


                    public void onCreate(SurfaceHolder surfaceHolder)
                    {
                            super.onCreate(surfaceHolder);
                    }

                    @Override
                    public void onVisibilityChanged(boolean visible)
                    {
                            this.visible = visible;

                            if (visible) 
                            {
                                handler.post(drawRunner);
                            }
                            else 
                            {
                                handler.removeCallbacks(drawRunner);
                            }
                    }

                    @Override
                    public void onSurfaceDestroyed(SurfaceHolder holder)
                    {
                            super.onSurfaceDestroyed(holder);
                            this.visible = false;
                            handler.removeCallbacks(drawRunner);
                    }

                    public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
                    {
                            draw();
                    }

                    void draw() 
                    {
                            final SurfaceHolder holder = getSurfaceHolder();
                            int xVelocity = 10;

                            Canvas c = null;
                            try 
                            {
                                    c = holder.lockCanvas();

                                    c.drawColor(Color.BLACK);
                                    if (c != null)
                                    {

                                            c.drawBitmap(backgroundImage, 0, 0, null);

                                            c.drawBitmap(image1, x,y, null);

                                            int width=c.getWidth();
                                            x += xVelocity;


                                            if(x>=width) 

                                            {xVelocity = -xVelocity;
                                            }


                                            }


                            }
                            finally 
                            {
                                    if (c != null)
                                           holder.unlockCanvasAndPost(c);
                            }

                            handler.removeCallbacks(drawRunner);
                            if (visible) 
                            {
                                      handler.postDelayed(drawRunner, 10); 
                            }    

                    }
            }
    }

The ic_launcher won't bounce back when it met the edge of the screen(it just keep moving to the right until I can't see it any more), can some one help me about it? ic_launcher碰到屏幕边缘时不会反弹(它一直向右移动,直到我再也看不到它了),有人可以帮我吗? I guess the problem is in this few lines 我想问题出在这几行

    int width=c.getWidth();
    x += xVelocity;

    if(x>=width) 
    {xVelocity = -xVelocity;
    }

    } 

I'm still a newbie in android programming, Thanks for helping me :) 我仍然是android编程的新手,感谢您的帮助:)

Following on from your comments: you are resetting velocity each time you call draw() ; 接下来是您的评论:每次调用draw()时都将重置速度;

move int xVelocity = 10; 移动int xVelocity = 10;

so it is at the top with int x,y; 所以它在顶部是int x,y;

 int x,y;
 int xVelocity = 10;
 int velValue = 10;
 int velInvValue = -10;

Then keep some better inverse code like: 然后保留一些更好的逆代码,例如:

if(x >= width)
{
      xVelocity = velInvValue;
}
else if(x <= 0)
{
      xVelocity = velValue;
}
x += xVelocity;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM