如何关闭OpenGL Activity

时间:2012-09-22 07:49:57

标签: java android android-activity

我的活动有问题,显示了CUBE 3D(简单的OpenGL示例)。当我按下手机或模拟器上的按键时,它应该返回主菜单,但不会返回任何反应。任何线索?

以下是代码:

public class Graphic3D extends Activity {
   private GLSurfaceView glView;  // Use subclass of GLSurfaceView (NEW)

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // Allocate a custom subclass of GLSurfaceView (NEW)
      glView = new MyGLSurfaceView(this);
      setContentView(glView);  // Set View (NEW)
   }


@Override
public void onBackPressed() {
    super.onBackPressed();
    // ????????
}


@Override
   protected void onPause() {
      super.onPause();
      glView.onPause();

   }

   @Override
   protected void onResume() {
      super.onResume();
      glView.onResume();
   }
}

MySurfaceView类:

public class MyGLSurfaceView extends GLSurfaceView {
   Graphics3DRenderer renderer;    // Custom GL Renderer

   // For touch event
   private final float TOUCH_SCALE_FACTOR = 180.0f / 320.0f;
   private float previousX;
   private float previousY;

   // Constructor - Allocate and set the renderer
   public MyGLSurfaceView(Context context) {
      super(context);
      renderer = new Graphics3DRenderer(context);
      this.setRenderer(renderer);
      // Request focus, otherwise key/button won't react
      this.requestFocus();  
      this.setFocusableInTouchMode(true);
   }

   // Handler for key event
   @Override
   public boolean onKeyDown(int keyCode, KeyEvent evt) {
      switch(keyCode) {
         case KeyEvent.KEYCODE_DPAD_LEFT:   // Decrease Y-rotational speed
            renderer.speedY -= 0.3f;
            break;
         case KeyEvent.KEYCODE_DPAD_RIGHT:  // Increase Y-rotational speed
            renderer.speedY += 0.3f;
            break;
         case KeyEvent.KEYCODE_DPAD_UP:     // Decrease X-rotational speed
            renderer.speedX -= 0.3f;
            break;
         case KeyEvent.KEYCODE_DPAD_DOWN:   // Increase X-rotational speed 
            renderer.speedX += 0.3f;
            break;
         case KeyEvent.KEYCODE_VOLUME_DOWN:// Zoom out (decrease z)
            renderer.z -= 0.4f;
            break;
         case KeyEvent.KEYCODE_VOLUME_UP: // Zoom in (increase z)
            renderer.z += 0.4f;
            break;
         case KeyEvent.KEYCODE_DPAD_CENTER:
             renderer.speedY = 0;
             renderer.speedX = 0;
             renderer.z      = -6.0f;
             break;
      }
      return true;  // Event handled
   }

   // Handler for touch event
   @Override
   public boolean onTouchEvent(final MotionEvent evt) {
      float currentX = evt.getX();
      float currentY = evt.getY();
      float deltaX, deltaY;
      switch (evt.getAction()) {
         case MotionEvent.ACTION_MOVE:
            // Modify rotational angles according to movement
            deltaX = currentX - previousX;
            deltaY = currentY - previousY;
            renderer.angleX += deltaY * TOUCH_SCALE_FACTOR;
            renderer.angleY += deltaX * TOUCH_SCALE_FACTOR;
      }
      // Save current x, y
      previousX = currentX;
      previousY = currentY;
      return true;  // Event handled
   }
}

1 个答案:

答案 0 :(得分:0)

在活动中,调用finish()将关闭当前活动。如果您还没有关闭主菜单Activity,它应该“打开”自己。

如果您需要再次启动活动,例如,如果您的菜单活动已关闭,您可以执行以下操作。

Intent i = new Intent(getApplicationContext(), Menu.class);
startActivity(i);
finish();