Android的OpenGL ES的2:如何在主要活动使用OpenGL的活性片段 [英] Android OpenGL ES 2: How to use an OpenGL activity as a fragment in the main activity
问题描述
我很新到Android和OpenGL ES。我要创建在OpenGL GUI和我想的主要活动使用它作为一个片段
。为了学习如何做到这一点,我试过2教程 - 此片段教程和上的OpenGL ES Android开发教程。
I am quite new to Android and OpenGL ES. I have to create a GUI in OpenGL and I would like to use it as a Fragment
in the main activity. In order to learn how to do this, I tried 2 tutorials - this Fragment tutorial and the Android developer tutorial on OpenGL ES.
但我还是不明白,我究竟该怎么办包括在片段
一个OpenGL视图。所以这个过程对我来说是相当混乱的OpenGL不使用XML布局文件。我愿做这样的事情:从片段
教程的主要活动里面我想包括第三片段
用OpenGL。去容易对我,我是一个初学者:)
But still I don't understand how exactly do I include an OpenGL view in a Fragment
. OpenGL doesn't use XML layout files so this process is quite confusing for me. I would like to do something like this: inside the main activity from the Fragment
tutorial I want to include a third Fragment
with OpenGL. Go easy on me I am a beginner :)
推荐答案
如果开发者教程是什么去了,那么下面的设置将工作:
If the developer tutorial is anything to go by, then the following setup would work:
活动:
public class MainActivity extends FragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount == 0)
{
finish();
}
}
});
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.main_container, new OpenGLFragment()).addToBackStack(null).commit();
}
}
}
活动XML(activity_main.xml中):
Activity XML (activity_main.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
片段:
public class OpenGLFragment extends Fragment
{
private GLSurfaceView mGLView;
public OpenGLFragment()
{
super();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
mGLView = new MyGLSurfaceView(this.getActivity()); //I believe you may also use getActivity().getApplicationContext();
return mGLView;
}
}
和我猜你需要使自己的GLSurfaceView作为教程说:
And I guess you need to make your own GLSurfaceView as the tutorial says:
class MyGLSurfaceView extends GLSurfaceView {
public MyGLSurfaceView(Context context){
super(context);
setEGLContextClientVersion(2);
// Set the Renderer for drawing on the GLSurfaceView
setRenderer(new MyRenderer());
}
}
和作为教程说,让你的渲染器:
And as the tutorial says, make your renderer:
public class MyGLRenderer implements GLSurfaceView.Renderer {
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
}
这篇关于Android的OpenGL ES的2:如何在主要活动使用OpenGL的活性片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!