捏/缩放效果到android中的3D对象

时间:2013-01-25 06:03:04

标签: pinchzoom

我对android更新一点。我想简单地用手指将捏合/缩放效果应用于3D对象。我在ImageView上为图像引用了捏合/缩放效果。我试图在GLSurfaceView上显示3D对象(如tringle,cube)。可以显示对象,甚至可以旋转但我无法应用捏合/缩放效果来缩放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立方体进行缩放效果?我是如此坚持这个问题,我的进一步工作停止了。任何帮助将不胜感激。谢谢 提前

0 个答案:

没有答案