Android opengl 正方形 手势三维旋转、缩小、放大

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;
import android.opengl.GLU;
import javax.microedition.khronos.opengles.GL10;
/**
 * Created by Administrator on 2017/6/1.
 */
//
public class SquareMyRederer extends  AbstractMyRenderer {
    @Override
    public void onDrawFrame(GL10 gl) {
        //清除颜色缓冲区
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        //设置绘图颜色
        gl.glColor4f(1f, 0f, 0f, 1f);


        //操作模型视图矩阵
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        //设置眼球的参数
        GLU.gluLookAt(gl,0f,0f,5f, 0f, 0f, 0f, 0f,1f,0f);


        //旋转角度
        gl.glRotatef(xrotate, 1, 0, 0);
        gl.glRotatef(yrotate, 0, 1, 0);
        //缩小放大
        gl.glScalef(XScalef,YScalef,ZScalef);


        //计算点坐标
        float r = 0.5f ;//半径
        float[] coords = {
                -r,r,0,
                -r,-r,0,
                r,r,0,
                r,-r,0,
        };
        //指定顶点指针
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, BufferUtil.ArryByteBuffer(coords));
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
    }
}

基类:

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) ;


}



你可能感兴趣的:(Android opengl 正方形 手势三维旋转、缩小、放大)