Могу ли я узнать, как изменить 3D-объект в GLSurfaceView при нажатии кнопки? Мое приложение закрывается всякий раз, когда я запускаю его на своем устройстве. У меня есть 2 класса, которые реализуют GLSurfaceView.Renderer, который является LessonOneRenderer и LessonOneRenderer2. И я помещаю этот код в кнопку onClickКак изменить 3D-объекты, когда кнопка была нажата на Android?
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);
}
}
Спасибо.
который ошибка, которую вы получили, пожалуйста, дайте журнал ошибок – Hardik
Это мой выход LogCat 11-02 18: 24: 51,972: E/AndroidRuntime (32224): FATAL ИСКЛЮЧЕНИЕ: главная 11-02 18: 24: 51.972: E/AndroidRuntime (32224): java.lang.IllegalStateException: setRenderer уже вызван для этого экземпляра. 11-02 18: 24: 51.972: E/AndroidRuntime (32224): at android.opengl.GLSurfaceView.checkRenderThreadState (GLSurfaceView.java:1795) 11-02 18: 24: 51.972: E/AndroidRuntime (32224): \t на android.opengl.GLSurfaceView.setRenderer (GLSurfaceView.java:347) 11-02 18: 24: 51,972: Е/AndroidRuntime (32224): \t на com.example.test3.MainActivity.onClick (MainActivity.java: 93) – AuroraBlaze
Вы устанавливаете время повторного размножения, чтобы оно произошло. Вы можете установить рендер только на GLSurfaceView один раз. идите с ответом @Sipka. – Hardik