MainActivity 类
package com.example.administrator.opengl_1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnTouchListener ,GestureDetector.OnGestureListener {
private float tatio;
private AbstractMyRenderer render;
private MyGLSurfaceView view;
private static final int DISTANCE = 50;
private static final int VELOCITY = 0;
private double nLenStart = 0;
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new MyGLSurfaceView(this);
render =new SquareMyRederer();
mGestureDetector = new GestureDetector(this);
view.setOnTouchListener(this);
view.setRenderer(render);
setContentView(view);
view.setFocusable(true);
view.setClickable(true);
view.setLongClickable(true);
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Log.i("MotionEvent","onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i("MotionEvent","onSingleTapUp");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i("MotionEvent","onScroll"+(e1.getX()-e2.getX()));
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("MotionEvent"," onLongPress");
}
//滑动旋转
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float step = 5f ;
if (e1.getX()-e2.getX()>DISTANCE&&Math.abs(velocityX)>VELOCITY){
render.yrotate = render.yrotate + step ;
Log.i("render",""+render.yrotate);
}else if (e2.getX()-e1.getX()>DISTANCE&&Math.abs(velocityX)>VELOCITY){
render.yrotate = render.yrotate - step ;
}else if (e1.getY()-e2.getY()>DISTANCE&&Math.abs(velocityY)>VELOCITY){
render.xrotate = render.xrotate - step ;
}else if (e2.getY()-e1.getY()>DISTANCE&&Math.abs(velocityY)>VELOCITY){
render.xrotate = render.xrotate + step ;
}
Log.i("MotionEvent",""+(e1.getX()-e2.getX()));
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
//手势是渲染图变大小
float scale = 0.05f ;
int nCnt = event.getPointerCount();
if( (event.getAction()&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN && 2 == nCnt)
{
int xlen = Math.abs((int)event.getX(0) - (int)event.getX(1));
int ylen = Math.abs((int)event.getY(0) - (int)event.getY(1));
nLenStart = Math.sqrt((double)xlen*xlen + (double)ylen * ylen);
}else if( (event.getAction()&MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_UP && 2 == nCnt)
{
int xlen = Math.abs((int)event.getX(0) - (int)event.getX(1));
int ylen = Math.abs((int)event.getY(0) - (int)event.getY(1));
double nLenEnd = Math.sqrt((double)xlen*xlen + (double)ylen * ylen);
//通过两个手指开始距离和结束距离,来判断放大缩小
if(nLenEnd > nLenStart){
render.XScalef=render.XScalef+scale;
render.YScalef=render.YScalef+scale;
render.ZScalef=render.ZScalef+scale;
Toast.makeText(getApplicationContext(), "放大", Toast.LENGTH_LONG).show();
}
else {
render.XScalef=render.XScalef-scale;
render.YScalef=render.YScalef-scale;
render.ZScalef=render.ZScalef-scale;
Toast.makeText(getApplicationContext(), "缩小", Toast.LENGTH_LONG).show();
}
}
return mGestureDetector.onTouchEvent(event);
}
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context) {
super(context);
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
}
SquareMyRederer类
package com.example.administrator.opengl_1;
基类:
package com.example.administrator.opengl_1; import android.opengl.GLSurfaceView; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; /** * Created by Administrator on 2017/6/1. */ public abstract class AbstractMyRenderer implements GLSurfaceView.Renderer{ public float ratio; public float xrotate = 0f;//围绕x轴旋转角度 public float yrotate = 0f;//围绕x轴旋转角度 public float XScalef = 1f;//缩放大小 public float YScalef = 1f;//缩放大小 public float ZScalef = 1f;//缩放大小 @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // 设置背景颜色 gl.glClearColor(0,0,0,1); //启用顶点缓冲区 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { //设置视口 gl.glViewport(0, 0, width, height); ratio = (float)width / (float)height; //投影矩阵 gl.glMatrixMode(GL10.GL_PROJECTION); //加载单位矩阵 gl.glLoadIdentity(); //设置平截头体 gl.glFrustumf(-ratio, ratio, -1, 1, 3f, 7f); } @Override public abstract void onDrawFrame(GL10 gl) ; }