有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Opengl ES 2.0中的java旋转和缩放图片

我正试着围绕图片中心旋转图片,并将其缩放到手机的屏幕分辨率(安卓)

这是我的密码:

// ----------------------------------------
// Scaling:
float scaleWidth = 2/screenWidth;
float scaleHeight = 2/screenHeight;

// ----------------------------------------
// Start position of the picture
int x = 60;
int y = 60;
float startX = x*scaleWidth;
float startY = y*scaleHeight;
// End position of the picture
float endX = (x + bmpWallSize) * scaleWidth;
float endY = (y + bmpWallSize) * scaleHeight;
// Center of the bitmap:
float midX = (x + (bmpWallSize/2)) * scaleWidth;
float midY = (y + (bmpWallSize/2)) * scaleHeight;

// ----------------------------------------
// Rotating:
gl.glTranslatef(midX, midY, 0f);
gl.glRotatef(r,0, 0,-1 );
gl.glTranslatef(-midX, -midY, 0);

r++; if (r > 360) r = 1;

不旋转图片看起来很好,但一旦旋转它就会改变大小(正方形变成矩形)。 我想这是因为我需要在旋转之后而不是之前缩放图片的顶点,但我不知道如何缩放。我在谷歌上搜索了很多,但找不到答案。谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    我找到了一个解决方案:

    此函数可以计算立方体的旋转角点:

    public Point GetRotatedRecPoint(int degree, int cubeSize, Point centerPoint) {
        degree = 360 - degree;
        float length = (float) Math.sqrt(((cubeSize/2)*(cubeSize/2)) + ((cubeSize/2)*(cubeSize/2)));
        float x = Math.cos(Math.toRadians(degree));
        float y = Math.sin(Math.toRadians(degree))
        return new Point(x*length + centerPoint.x, y*length + centerPoint.y);
    }
    

    像这样:

    // The degrees of each Corner of the Cube
    int topLeftDegree = r+45;
    int topRightDegree = r+135;
    int botRightDegree = r+225;
    int botLeftDegree = r+315;
    
    Point topLeftPoint = new Point(GetRotatedRecPoint(topLeftDegree, cubeSize, centerPoint));
    Point topRightPoint = new Point(GetRotatedRecPoint(topRightDegree, cubeSize, centerPoint));
    Point botRightPoint = new Point(GetRotatedRecPoint(botRightDegree, cubeSize, centerPoint));
    Point botLeftPoint = new Point(GetRotatedRecPoint(botLeftDegree, cubeSize, centerPoint));
    

    之后,我对其进行缩放:

    topLeftPoint.x = topLeftPoint.x * widthScale;
    topLeftPoint.y = topLeftPoint.y * heightScale;
    topRightPoint.x = topRightPoint.x * widthScale;
    .....
    

    将其添加到verticeBuffer,然后绘制。 这在不使用glTranslatef()或glRotatef()的情况下有效。 我相信这不是最好的解决方案,但对我来说很好^