1

我的活动显示 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
   }
}
4

1 回答 1

0

在一个 Activity 中,调用finish()会关闭当前的 Activity。如果您还没有关闭主菜单 Activity,它应该会自行“打开”。

如果您需要重新启动一个 Activity,例如如果您的菜单 Activity 关闭,您可以执行以下操作。

Intent i = new Intent(getApplicationContext(), Menu.class);
startActivity(i);
finish();
于 2012-09-22T07:53:12.853 回答