我可以知道如何在单击按钮时更改 GLSurfaceView 中的 3D 对象吗?每当我在我的设备上运行它时,我的应用程序就会一直关闭。我有 2 个实现 GLSurfaceView.Renderer 的类,即 LessonOneRenderer 和 LessonOneRenderer2。我把这段代码放在按钮 onClick
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
mGLSurfaceView.setRenderer(new LessonOneRenderer2());
但它不起作用。
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id= "@+id/linearlayout1" >
<Button
android:id="@+id/buttonID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="A Button" />
<com.example.test3.MyGLSurfaceView
android:id="@+id/glSurfaceViewID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23" />
</LinearLayout>
===============
package com.example.test3;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements android.view.View.OnClickListener
{
/** Hold a reference to our GLSurfaceView */
private GLSurfaceView mGLSurfaceView;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//mGLSurfaceView = new GLSurfaceView(this);
//setContentView(R.layout.activity_main);
//mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (supportsEs2)
{
// Request an OpenGL ES 2.0 compatible context.
//mGLSurfaceView.setEGLContextClientVersion(2);
// Set the renderer to our demo renderer, defined below.
//mGLSurfaceView.setRenderer(new LessonOneRenderer());
//mGLSurfaceView.setRenderer
setContentView(R.layout.activity_main);
//mGLSurfaceView.setRenderer(new LessonOneRenderer());
//MyGLSurfaceView.mRenderer = new LessonOneRenderer();
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
mGLSurfaceView.setRenderer(new LessonOneRenderer());
}
else
{
// This is where you could create an OpenGL ES 1.x compatible
// renderer if you wanted to support both ES 1 and ES 2.
return;
}
//setContentView(R.layout.activity_main);
//setContentView(mGLSurfaceView);
btn = (Button)findViewById(R.id.buttonID);
btn.setOnClickListener(this);
}
@Override
protected void onResume()
{
// The activity must call the GL surface view's onResume() on activity onResume().
super.onResume();
mGLSurfaceView.onResume();
}
@Override
protected void onPause()
{
// The activity must call the GL surface view's onPause() on activity onPause().
super.onPause();
mGLSurfaceView.onPause();
}
public void onClick(View v)
{
if(v == btn)
{
//finish();
mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
mGLSurfaceView.setRenderer(new LessonOneRenderer2());
}
}
}
=======================
package com.example.test3;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
public class MyGLSurfaceView extends GLSurfaceView{
public MyGLSurfaceView(Context context) {
super(context);
setEGLContextClientVersion(2);
}
public MyGLSurfaceView(Context context, AttributeSet attrs){
super(context,attrs);
setEGLContextClientVersion(2);
}
}
谢谢你。