Android 动态壁纸 - onOffsetsChanged
有没有更好的方法可以在画布内移动位图,而无需每次都重新绘制位图?
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你问:有更好的办法吗?
答案是:不。您需要重新绘制整个场景、背景以及所有每一帧。
另外,一般来说,你不想在 onOffsetsChanged 中绘制;只需更新您在可运行程序中使用的变量即可。看一下 SDK 中的 Cube 示例是如何工作的。乔伊的例子也是正确的。希望这有帮助
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; just update variables that you're using in your runnable. Take a look at how the Cube example in the SDK works. Joey's example is correct, too. Hope this helps
您可以像我通常在通过偏移量更改来读取和修改位置或图像的情况下所做的那样。我使用 onOffsetsChanged 将信息提供给常量变量,该变量在绘图例程发生变化时会自动更新。
首先定义您想要使用的常量,如下所示...
在本示例中,我捕获 onOffsetsChanged 事件中的所有值,您只需定义和捕获将使用的偏移量、步长或像素。
一旦您获得了这些数据,它将为您在应用程序中使用提供更多的功能和灵活性。您现在可以像下面这样调用画布更新,当每次调用绘制例程时偏移量发生变化时,您可以使用这些数字来修改图像的位置、移动速度或您想要的任何其他数学计算。
等等。希望这就是您所问的问题,我并不完全清楚。如果您有需要,这还使您能够在其他地方使用这些偏移、步骤和位置。
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.
Start by defining the constants you want to work with like so...
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.
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.
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.