Как вращать по кругу (на 360 °) многоугольник (квадрат), касаясь экрана?

#android #opengl-es #rotation

#Android #opengl-es #вращение

Вопрос:

На самом деле у меня есть приложение, которое создает квадрат (многоугольник, а не куб, квадрат), и пользователь может вращать его, касаясь экрана, но что-то идет не так, потому что вращение несправедливо, оно может вращать его только по горизонтали и вертикали, но не по кругу. Мне нужно, чтобы пользователь тоже мог вращать его по кругу.

Я использую этот код для поворота (по горизонтали / вертикали)

 public void onDrawFrame(GL10 gl) {
    //Clear Screen And Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    gl.glLoadIdentity();                    //Reset The Current Modelview Matrix

    //Drawing
    gl.glTranslatef(0.0f, 0.0f, z);         //Move z units into the screen
    gl.glScalef(0.8f, 0.8f, 0.8f);          //Scale the Cube to 80 percent, otherwise it would be too large for the screen

    //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y

    square.draw(gl);                    //Draw the Cube 

    //Change rotation factors
    xrot  = xspeed;
    yrot  = yspeed;
}

public boolean onTouchEvent(MotionEvent event) {
        //
        float x = event.getX();
        float y = event.getY();

        //If a touch is moved on the screen
        if(event.getAction() == MotionEvent.ACTION_MOVE) {
            //Calculate the change
            float dx = x - oldX;
            float dy = y - oldY;
            //Define an upper area of 10% on the screen
            int upperArea = this.getHeight() / 10;

            //Zoom in/out if the touch move has been made in the upper
            if(y < upperArea) {
                z -= dx * TOUCH_SCALE / 2;

            //Rotate around the axis otherwise
            } else {                
                xrot  = dy * TOUCH_SCALE;
                yrot  = dx * TOUCH_SCALE;
            }        

        //A press on the screen
        } else if(event.getAction() == MotionEvent.ACTION_UP) {
            //Define an upper area of 10% to define a lower area
            int upperArea = this.getHeight() / 10;
            int lowerArea = this.getHeight() - upperArea;

            //Change the light setting if the lower area has been pressed 
            if(y > lowerArea) {

            }
        }

        //Remember the values
        oldX = x;
        oldY = y;

        //We handled the event
        return true;
    }
  

Ответ №1:

Вы должны попробовать это:

   yourRootLayout.setOnClickListener(yourListener);
  yourAnimation = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  yourAnimation.setDuration(duration in milliseconds - int);

@Override

protected void onClick(View view){

   if (view.getId() == R.id.yourRootLayoutId){

       yourRootLayout.startAnimation(yourAnimation);

   }

}