English 中文(简体)
GLSurfaceView 如何和理解拖动和缩放事件
原标题:How-to and Understanding of drag and zoom event with GLSurfaceView

我用这个测试应用程序做了所有工作, 使用标准顶点、颜色和点缓冲显示 3D 对象 。

我可以......

  • rotate the object with touch events
  • render object with openGL ES 1.0 using GL10
  • everything works great

现在我想用两根手指来放大和外放大“ 平方” 运动。 我找到了一些好的辅导和样本代码, 但大部分是图像观察。 我对安卓还是半新, 我并不完全理解如何让这个缩放功能为 GLSurfaceView 工作 。

下面是我至今为止对活动类的代码。 第一个触摸处理器是用于我通过为图像View设计的一个教程发现的缩放活动(我使用这个教程是因为我认为这很容易转换) 。 第二个处理器是用于旋转活动,这个活动效果很好。

Thank you ahead of time and I hope this post will help others with the same issue. I will be standing by for any edits or additions needed.

 // Activity for rendering 3D object openGL ES 1.0
 public class GL_ExampleActivity extends Activity
 {
      private GLSurfaceView surface;

      @Override
      public void onCreate(Bundle savedInstanceState)
      {
           super.onCreate(savedInstanceState);

           surface = new GL_ExampleSurfaceView(this);
           setContentView(surface);
      }

      @Override
      protected void onPause()
      {
           // TODO Auto-generated method stub
           super.onPause();
           surface.onPause();
      }

      @Override
      protected void onResume()
      {
           // TODO Auto-generated method stub
           super.onResume();
           surface.onResume();
      }
 }

 class GL_ExampleSurfaceView extends GLSurfaceView
 {
      private static final String TAG = "Touch";

      Matrix matrix_new = new Matrix();
      Matrix last_matrix = new Matrix();

      static final int NONE = 0;
      static final int DRAG = 1;
      static final int ZOOM = 2;
      int mode = NONE;

      PointF start = new PointF();
      PointF mid = new PointF();
      float oldDist = 1f;


      private final float SCALE_FACTOR = 180.0f / 320;
      private GLRenderer renderer;
      private float previous_x;
      private float previous_y;

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

           renderer = new GLRenderer();
           setRenderer(renderer);

           setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
      }

      @Override
      public boolean onTouchEvent(MotionEvent e)
      {
           // handler for drag and zoom events
           switch (e.getAction() & MotionEvent.ACTION_MASK)
           {
                case MotionEvent.ACTION_DOWN:
                     last_matrix.set(matrix_new);
                     start.set(e.getX(), e.getY());
                     Log.d(TAG, "mode=DRAG");
                     mode = DRAG;
                     //requestRender();
                     break;
                case MotionEvent.ACTION_POINTER_DOWN:
                     oldDist = finger_distance(e);
                     Log.d(TAG, "oldDist=" + oldDist);
                     if (oldDist > 10f)
                     {
                         last_matrix.set(matrix_new);
                         finger_distance_midpoint(mid, e);
                         mode = ZOOM;
                         Log.d(TAG, "mode=ZOOM");
                     }
                     //requestRender();
                     break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                     mode = NONE;
                     Log.d(TAG, "mode=NONE");
                     //requestRender();
                     break;
                case MotionEvent.ACTION_MOVE:
                     if (mode == DRAG)
                     {
                          matrix_new.set(last_matrix);
                          matrix_new.postTranslate(e.getX() - start.x, e.getY() - start.y);
                     }
                     else if (mode == ZOOM)
                     {
                          float newDist = finger_distance(e);
                          Log.d(TAG, "newDist=" + newDist);
                          if (newDist > 10f)
                          {
                               matrix_new.set(last_matrix);
                               float scale = newDist / oldDist;
                               matrix_new.postScale(scale, scale, mid.x, mid.y);
                          }
                     }
                    //requestRender();
                    break;
            }
           //view.setImageMatrix(matrix_new);



           // handler for rotation event, y and x axis
           float x = e.getX();
           float y = e.getY();

           switch (e.getAction())
           {
                case MotionEvent.ACTION_MOVE:

                float dx = x - previous_x;
                float dy = y - previous_y;

                if (y > getHeight() / 2)
                {
                     dx = dx * -1 ;
                }

                if (x < getWidth() / 2)
                {
                     dy = dy * -1 ;
                }
                renderer.angle_x += dx * SCALE_FACTOR;
                renderer.angle_y += dy * SCALE_FACTOR;
                requestRender();
           }
           previous_x = x;
           previous_y = y;
           return true;
      }


      private float finger_distance(MotionEvent e)
      {
           float x = e.getX(0) - e.getX(1);
           float y = e.getY(0) - e.getY(1);
           return FloatMath.sqrt(x * x + y * y);
      }

      private void finger_distance_midpoint(PointF point, MotionEvent e)
      {
           float x = e.getX(0) + e.getX(1);
           float y = e.getY(0) + e.getY(1);
           point.set(x / 2, y / 2);
      }

 }

再次感谢...

问题回答

处理此选项有两种显而易见的方法。 如果您只想增加单个对象的大小, 您可以在绘制对象时使用 glServicef 。 使用 scalef 很容易, 比较简单, 但这并不是您想要的 。

另一种解决办法是对您的预测矩阵应用一个缩放因子,该因子将产生预期效果。要做到这一点,只需在绘制前将您的预测矩阵的左、右、上和下距离乘以类似比例尺。

gl.glFrustumf(width/2 * zoom, width/2 * zoom, height/2 * zoom, height/2 * zoom, 1, -1);
(Note, if you re using an orthogonal matrix you will be using glOrthof instead)

如果您有兴趣,请查看有关投影矩阵 的更多信息,http://www.songho.ca/opengl/gl_projectionmatrix.html" rel=“no follow'>这里





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签