Android:如何根据目标坐标旋转移动的动画精灵

bwa*_*ags 6 android sprite-sheet image-rotation

我的应用程序在Canvas周围激活精灵实例,然后在屏幕上朝着ax/y坐标移动.我希望能够围绕其中心旋转精灵,使其面向目标坐标.我正在使用精灵表,并有剪切问题.我也找到了很多很好的例子,但似乎没有任何东西可以覆盖我正在寻找的东西.这个例子非常接近,但为了提高效率,我使用的是ImagePooler类,无法在每次绘制/旋转时重新加载图像.因此,如果有人知道如何旋转预装的图像w/out切割我的精灵表,我将非常感激.

Lum*_*mis 14

首先,很容易旋转精灵,你可以使用画布或矩阵:

Matrix matrix = new Matrix();
matrix.postRotate(angle, (ballW / 2), (ballH / 2)); //rotate it
matrix.postTranslate(X, Y); //move it into x, y position
canvas.drawBitmap(ball, matrix, null); //draw the ball with the applied matrix

// method two 
canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (ballW / 2), Y + (ballH / 2)); //rotate the canvas' matrix
canvas.drawBitmap(ball, X, Y, null); //draw the ball on the "rotated" canvas
canvas.restore(); //rotate the canvas' matrix back
//in the second method only the ball was roteded not the entire canvas
Run Code Online (Sandbox Code Playgroud)

要将它转向目的地,您需要知道精灵和目的地之间的角度:

spriteToDestAngle =  Math.toDegrees(Math.atan2((spriteX - destX)/(spriteY - destY)));
Run Code Online (Sandbox Code Playgroud)

现在你需要做的就是使用这个角度进行精灵旋转,并使用像angleShift这样的常量调整它,这取决于你的精灵最初指向的位置.

我不确定这是否有用,但希望它可以给你一些想法......