问题描述
如何在Pixmap上绘制图像,但是旋转一定角度,就像在Batch上绘制时一样?没有像批处理绘制方法那样以角度为参数的Pixmap方法.
How can I draw an image to Pixmap, but rotated by some angle, like I can do when drawing on Batch? There is no Pixmap method that has an angle as parameter, like batch drawing methods have.
推荐答案
好,这是我设法制作的代码(实际上是从这里借来并进行调整的代码:):
Ok, here is the code I managed to make (actually to borrow and adjust from here: Rotate Bitmap pixels ):
public Pixmap rotatePixmap (Pixmap src, float angle){
final int width = src.getWidth();
final int height = src.getHeight();
Pixmap rotated = new Pixmap(width, height, src.getFormat());
final double radians = Math.toRadians(angle), cos = Math.cos(radians), sin = Math.sin(radians);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
final int
centerx = width/2, centery = height / 2,
m = x - centerx,
n = y - centery,
j = ((int) (m * cos + n * sin)) + centerx,
k = ((int) (n * cos - m * sin)) + centery;
if (j >= 0 && j < width && k >= 0 && k < height){
rotated.drawPixel(x, y, src.getPixel(j, k));
}
}
}
return rotated;
}
它创建并从传递的源Pixmap中返回旋转的Pixmap.它可以绕中心旋转,并且在PC和HTC Sensation上都非常快.
It creates and returns rotated Pixmap out of passed source Pixmap. It rotates around the center and it's actually pretty fast, on PC and on my HTC Sensation.
别忘了处理使用后得到的Pixmap.
And don't forget to dispose Pixmap you get after the usage.
为此提供更好的系统解决方案会更好,它具有旋转点坐标,但这对我有帮助,希望会对其他人有所帮助.
It would be nice to have system solution for this which would be more optimized, have rotation point coordinates, but this does the job for me, hopefully will be helpful for somebody else.
这篇关于如何在pixmap(LibGDX)上绘制旋转的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!