gpt4 book ai didi

XML 布局中的 Android GLSurfaceView

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:48 26 4
gpt4 key购买 nike

我试图将一个 GLSurfaceView 和一个按钮放入同一个 xml 布局中。但是每当我在我的设备上运行我的应用程序时,它都会自动关闭。有人可以帮助我并告诉我我缺少什么或我的代码有什么问题吗?

这是我的代码

===================

<?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.test2.MyGLSurfaceView
android:id="@+id/glSurfaceViewID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.23" />

</LinearLayout>

================

package com.example.test2;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends Activity {

private GLSurfaceView mGLView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create a GLSurfaceView instance and set it
// as the ContentView for this Activity
mGLView = new MyGLSurfaceView(this);
//setContentView(mGLView);
setContentView(R.layout.activity_main);
}

// @Override
protected void onPause() {
super.onPause();
mGLView = (MyGLSurfaceView)findViewById(R.id.glSurfaceViewID);
mGLView.onPause();
}
//
@Override
protected void onResume() {
super.onResume();
mGLView = (MyGLSurfaceView)findViewById(R.id.glSurfaceViewID);
mGLView.onResume();
}
}

class MyGLSurfaceView extends GLSurfaceView {

private final MyGLRenderer mRenderer;

public MyGLSurfaceView(Context context) {
super(context);

// Create an OpenGL ES 2.0 context.
setEGLContextClientVersion(2);

// Set the Renderer for drawing on the GLSurfaceView
mRenderer = new MyGLRenderer();
setRenderer(mRenderer);

// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
private float mPreviousX;
private float mPreviousY;

@Override
public boolean onTouchEvent(MotionEvent e) {
// MotionEvent reports input details from the touch screen
// and other input controls. In this case, you are only
// interested in events where the touch position changed.

float x = e.getX();
float y = e.getY();

switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:

float dx = x - mPreviousX;
float dy = y - mPreviousY;

// reverse direction of rotation above the mid-line
if (y > getHeight() / 2) {
dx = dx * -1 ;
}

// reverse direction of rotation to left of the mid-line
if (x < getWidth() / 2) {
dy = dy * -1 ;
}

mRenderer.mAngle += (dx + dy) * TOUCH_SCALE_FACTOR; // = 180.0f / 320
requestRender();
}

mPreviousX = x;
mPreviousY = y;
return true;
}

}

===========================

谢谢。

最佳答案

在 MainActivity.onCreate() 中使用 findViewById 而不是创建新 View 。(就像你在 onPause、onResume 中所做的那样)并且只分配一次变量。

setContentView(R.layout.activity_main);
mGLView = (MyGLSurfaceView) findViewById(R.id.glSurfaceViewID);

确保 MyGLSurfaceView 具有带 AttributeSet 的构造函数,以便它可以从 XML 中扩展。看看这个 stackoverflow atricle对于构造函数。

还要更改 XML 中 Button 和 MyGLSurfaceView 的顺序,因为现在 Button 将在布局中低于 MyGLSurfaceView,因此您将看不到它。

关于XML 布局中的 Android GLSurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19732720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com