效果图:
编写jiem.java
*指定屏幕所要显示的假面,并对见、界面进行相关设置
*为Activity设置恢复处理,当Acitvity恢复设置时显示界面同样应该恢复
*当Activity暂停设置时,显示界面同样应该暂停
package com.scout.eeeeeee; import android.app.Activity; import android.os.Bundle; import android.app.Activity; import android.os.Bundle; import android.app.Activity; import android.content.pm.ActivityInfo; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public class jiem extends Activity { private MyGLSurfaceView mGLSurfaceView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); mGLSurfaceView = new MyGLSurfaceView(this); setContentView(mGLSurfaceView); mGLSurfaceView.setFocusableInTouchMode(true);//设置为可触控 mGLSurfaceView.requestFocus();//获取焦点 } @Override protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); } }
编写MyGLSurfaceView.java实现场景加载和渲染功能
package com.scout.eeeeeee; /** * Created by liuguodong on 2017/10/29. */ import java.io.IOException; import java.io.InputStream; import android.opengl.GLSurfaceView; import android.opengl.GLUtils; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.MotionEvent; public class MyGLSurfaceView extends GLSurfaceView { private final float suo = 180.0f/320;//角度缩放比例 private SceneRenderer mRenderer;//场景渲染器 private float shangY;//上次的触控位置Y坐标 private float shangX;//上次的触控位置Y坐标 private int lightAngle=90;//灯的当前角度 public MyGLSurfaceView(Context context) { super(context); mRenderer = new SceneRenderer(); //创建场景渲染器 setRenderer(mRenderer); //设置渲染器 setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);//设置渲染模式为主动渲染 } //触摸事件回调方法 @Override public boolean onTouchEvent(MotionEvent e) { float y = e.getY(); float x = e.getX(); switch (e.getAction()) { case MotionEvent.ACTION_MOVE: float dy = y - shangY;//计算触控笔Y位移 float dx = x - shangX;//计算触控笔Y位移 mRenderer.cylinder.mAngleX += dy * suo;//设置沿x轴旋转角度 mRenderer.cylinder.mAngleZ += dx * suo;//设置沿z轴旋转角度 requestRender();//重绘画面 } shangY = y;//记录触控笔位置 shangX = x;//记录触控笔位置 return true; } private class SceneRenderer implements GLSurfaceView.Renderer { int textureId;//纹理名称ID zhuCH cylinder;//创建圆柱体 public SceneRenderer() { } public void onDrawFrame(GL10 gl) { //清除颜色缓存 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //设置当前矩阵为模式矩阵 gl.glMatrixMode(GL10.GL_MODELVIEW); //设置当前矩阵为单位矩阵 gl.glLoadIdentity(); gl.glPushMatrix();//保护变换矩阵现场 float lx=0; //设定光源的位置 float ly=(float)(7*Math.cos(Math.toRadians(lightAngle))); float lz=(float)(7*Math.sin(Math.toRadians(lightAngle))); float[] positionParamsRed={ lx,ly,lz,0}; gl.glLightfv(GL10.GL_LIGHT1, GL10.GL_POSITION, positionParamsRed,0); initMaterial(gl);<