3

我对android有点新。我想通过手指将捏合/缩放效果应用于 3D 对象。我在 ImageView 上提到了图像的捏合/缩放效果。我试图在 GLSurfaceView 上显示 3D 对象(如三角形、立方体)。可以显示对象,甚至可以旋转对象,但我无法应用捏合/缩放效果来缩放 3d 对象。

我列出了一些我浏览过的链接http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_3D.html http://insanitydesign.com/wp/projects/nehe-android -ports/ http://x-tutorials.blogspot.sg/2011/11/implement-pinch-zoom-in-ontouchlistener.html

我在这里提供我的代码...

公共类 MyGLActivity 扩展 Activity {

    private GLSurfaceView glView;   // Use GLSurfaceView
    public static Context context;

   // Call back when the activity is started, to initialize the view
   @Override
   protected void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);

      glView = new MyGLSurfaceView(this);           // Allocate a GLSurfaceView

      setContentView(glView);                // This activity sets to GLSurfaceView
   }


   // Call back when the activity is going into the background
   @Override
   protected void onPause() {
      super.onPause();
      //glView.onPause();
   }

   // Call back after onPause()
   @Override
   protected void onResume() {
      super.onResume();
     // glView.onResume();
   }


 class MyGLSurfaceView extends GLSurfaceView
   {
       private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
        private float mPreviousX;
        private float mPreviousY;

        // touch events
        private final int NONE = 0;
        private final int DRAG = 0;
        private final int ZOOM = 0;

        // pinch to zoom
        float oldDist = 100.0f;
        float newDist;

        int mode = 0;

        MyGLRenderer renderer;

        public MyGLSurfaceView(Context context) 
        {
            super(context);

            renderer=new MyGLRenderer();
            setRenderer(renderer);


            // Render the view only when there is a change in the drawing data
            setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        }

    public boolean onTouchEvent(MotionEvent event)
    {
        //Log.e("tag", "Touched");
        //Toast.makeText(this.getContext(), "Touched",Toast.LENGTH_SHORT).show();


        float x = event.getX();
        float y = event.getY();
        switch (event.getAction())
        {
                case MotionEvent.ACTION_DOWN:           // one touch: drag
                    mode = DRAG;
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:   // two touches: zoom
                    oldDist = spacing(event);
                    if (oldDist > 10.0f) {
                        mode = ZOOM; // zoom
                    }
                    break;
                case MotionEvent.ACTION_UP:     // no mode
                    mode = NONE;
                    oldDist = 100.0f;
                    break;
                case MotionEvent.ACTION_POINTER_UP:     // no mode
                    mode = NONE;
                    oldDist = 100.0f;
                    break;
                case MotionEvent.ACTION_MOVE:                       // rotation
                    if (event.getPointerCount() > 1 && mode == ZOOM) 
                    {
                        newDist = spacing(event);
                        //Log.d("SPACING: ", "OldDist: " + oldDist + ", NewDist: " + newDist);
                        if (newDist > 10.0f) 
                        {
                            float scale = newDist/oldDist; // scale
                            // scale in the renderer




                            Log.i("Zoom", "Zooming......");

                            oldDist = newDist;
                        }
                    }
                    else if (mode == DRAG)
                    {
                        float dx = x - mPreviousX;
                        float dy = y - mPreviousY;
                        renderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
                        renderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
                        requestRender();
                    }
                    break;
                }
                mPreviousX = x;
                mPreviousY = y;
                return true;
        }
   }

    private float spacing(MotionEvent event)
    {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }

}

谁能给我一些演示示例来对 3d 立方体进行缩放效果?我被这个问题困住了,我的进一步工作停止了。任何帮助将不胜感激。提前致谢

4

0 回答 0