我用这个测试应用程序做了所有工作, 使用标准顶点、颜色和点缓冲显示 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);
}
}
再次感谢...