下面是详细讲解“Android基于OpenGL的GLSurfaceView创建一个Activity实现方法”的完整攻略。
前置知识
在学习本攻略前,建议您已经具备以下知识:
- Android基础知识、Java编程基础知识;
- 熟悉Android编程中Activity、View的相关知识;
- OpenGL ES的基本概念和使用方法。
创建GLSurfaceView
在Android中创建GLSurfaceView相对比较简单,只需要继承GLSurfaceView类并实现相应的生命周期方法即可。下面我们先来看一个简单的例子。
示例一
public class MyGLSurfaceView extends GLSurfaceView {
private MyGLRender myGLRender;
public MyGLSurfaceView(Context context) {
super(context);
init();
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// 设置OpenGL ES版本号为2.0
setEGLContextClientVersion(2);
// 设置渲染器
myGLRender = new MyGLRender();
setRenderer(myGLRender);
}
}
在上面的代码中,我们继承了GLSurfaceView类,并实现了类的两个构造函数,以及一个init()方法。在init()方法中,我们设置了OpenGL ES版本号为2.0,并实例化了一个MyGLRender对象,并将它设置为渲染器。
示例二
我们再来看一个稍微复杂一些的例子,这个例子可以通过手指在屏幕上移动的行为从不同角度观察物体。
public class MyGLSurfaceView extends GLSurfaceView {
private final float TOUCH_SCALE_FACTOR = 90.0f / 320;
private MyRenderer myRenderer;
private float mPreviousX;
private float mPreviousY;
public MyGLSurfaceView(Context context) {
super(context);
init();
}
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
// 设置OpenGL ES版本号为2.0
setEGLContextClientVersion(2);
myRenderer = new MyRenderer();
setRenderer(myRenderer);
// 设置渲染模式
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
float dx = x - mPreviousX;
float dy = y - mPreviousY;
myRenderer.setAngle(myRenderer.getAngle() +
((dx + dy) * TOUCH_SCALE_FACTOR));
requestRender();
}
mPreviousX = x;
mPreviousY = y;
return true;
}
}
在上面的代码中,我们添加了onTouchEvent()方法,并通过手指移动的行为改变了物体的角度。
创建Activity
创建Activity也很简单,首先需要在布局文件中添加一个GLSurfaceView组件,然后再Activity中将其实例化。下面我们来看一下示例代码。
示例一
<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.MyGLSurfaceView
android:id="@+id/glSurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private MyGLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
glSurfaceView = findViewById(R.id.glSurfaceView);
}
}
在上面的示例中,我们只是简单地将GLSurfaceView组件添加到了布局文件中,并在Activity中将其实例化,没有做任何其他的操作。
示例二
如果要增加一些交互功能,可以在Activity中添加一些按钮或者其他组件,来改变GLSurfaceView的行为。下面我们给出一个改变物体颜色的例子。
<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.MyGLSurfaceView
android:id="@+id/glSurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="@+id/button_change_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:onClick="onChangeColorClick" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private MyGLSurfaceView glSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
glSurfaceView = findViewById(R.id.glSurfaceView);
}
// 点击按钮时改变物体颜色
public void onChangeColorClick(View view) {
glSurfaceView.getRenderer().changeColor();
glSurfaceView.requestRender();
}
}
在上面的代码中,我们添加了一个按钮,并在其onClick事件中改变物体颜色,并让GLSurfaceView进行重新渲染。
总结
通过以上两个示例,我们了解了如何创建一个基于OpenGL的GLSurfaceView,并将其应用到一个Activity中。当然,这里的示例只是最基础的用法,实际应用中还有很多需要考虑的因素,例如如何优化性能、如何添加交互效果等等。希望这篇攻略对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android基于OpenGL的GLSurfaceView创建一个Activity实现方法 - Python技术站