简体   繁体   English

Android动态壁纸 - onOffsetsChanged

[英]Android Live wallpaper - onOffsetsChanged

Is there a better way to move the bitmap inside a canvas without redrawingBitmap every time, for every single one? 有没有更好的方法可以在画布内移动位图,而不是每次都重绘一次,每一个都有一个?

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


             final SurfaceHolder holder = getSurfaceHolder();                                                   
             Canvas c = null;
             try {
                  c = holder.lockCanvas();
                  if (c != null) {                                  
                            c.drawBitmap(this.ImageI, xPixels, 0, null);                                                                    
                            c.drawBitmap(this.ImageII, xPixels, 0, paint);  



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

            }

You can do like I usually do in a situation where you are reading and modifying a location or image via the offsets changing. 您可以像我通常那样在您通过偏移更改来阅读和修改位置或图像的情况下执行此操作。 I use onOffsetsChanged to feed information to a constant variable that is updated in my draw routine automatically as it changes. 我使用onOffsetsChanged将信息提供给一个常量变量,该变量在我的绘图例程中随着它的变化自动更新。

Start by defining the constants you want to work with like so... 首先定义你想要使用的常量,如下所示......

Private float mXOffset = 0,
              mYOffset = 0,
              mXStep   = 0,
              mYStep   = 0,
              mXPixels = 0,
              mYPixels = 0;

In this example I am capturing all of the values from the onOffsetsChanged event, you need only define and capture the offsets, steps, or pixels you will be using. 在此示例中,我将捕获onOffsetsChanged事件中的所有值,您只需定义并捕获将要使用的偏移量,步长或像素。

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

mXOffset = xOffset;
mYOffset = yOffset;
mXStep   = xStep;
mYStep   = yStep;
mXPixels = xPixels;
mYPixels = yPixels; 

DrawStuff();   
}

Once you have this data it will give you a little more power and flexibility to use in your application. 获得此数据后,它将为您的应用程序提供更多功能和灵活性。 You can call your canvas updates now like the following and when the offsets change each time your draw routine is called you can use these numbers to modify location of image, speed of movement, or any other mathematical calculation you would like. 您现在可以像下面这样调用画布更新,并且每次调用绘制例程时偏移都会更改,您可以使用这些数字来修改图像的位置,移动速度或您想要的任何其他数学计算。

private void DrawStuff(){
        final SurfaceHolder holder = getSurfaceHolder();                                                   
             Canvas c = null;
             try {
                  c = holder.lockCanvas();
                  if (c != null) {                                  
                      c.drawBitmap(this.ImageI, mXPixels,mYPixels + mYOffset,null);                                                                    
                      c.drawBitmap(this.ImageII, xPixels, 0, paint);  
                                 }
                 } finally {
                    if (c != null) holder.unlockCanvasAndPost(c);
                }

    }

and so on and so forth. 等等等等。 Hope this is what you was asking I wasn't entirely clear. 希望这就是你所问的我并不完全清楚。 This will also give you the ability to use these offsets, steps, and locations elsewhere if you have a need. 如果您有需要,这还可以让您在其他地方使用这些偏移,步骤和位置。

You are asking: is there a better way? 你在问:有更好的方法吗? The answer is: no. 答案是不。 You need to redraw your whole scene, background and all, every frame. 您需要重绘整个场景,背景和所有帧。 Also, generally speaking, you don't want to draw in onOffsetsChanged; 另外,一般来说,你不想画入onOffsetsChanged; just update variables that you're using in your runnable. 只需更新您在runnable中使用的变量。 Take a look at how the Cube example in the SDK works. 看看SDK中Cube示例的工作原理。 Joey's example is correct, too. 乔伊的例子也是正确的。 Hope this helps 希望这可以帮助

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

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