如何在Android上单击按钮时更改3D对象?

时间:2013-11-02 07:00:20

标签: android opengl-es glsurfaceview

我可以知道如何在单击按钮时更改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);

    }

}

谢谢。

1 个答案:

答案 0 :(得分:0)

您只能在GLSurfaceView上设置渲染器一次

而且,如果在onCreate中分配mGLSurfaceView,则不必再次在onClick中分配它。 意思是你不必打电话

mGLSurfaceView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);
再次点击ont,因为你已经获得了onCreate中的值。 你能做的是:

LessonOneRenderer renderer = new LessonOneRenderer ();//keep reference of the renderer you set
mGLSurfaceView.setRenderer(renderer);
...
public void onClick(View v){
    renderer.click();//do 3D transformation here.
}