- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图将一个 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/
我有一个增强现实应用程序,它显示相机 View 和一个带有多边形的 GLSurfaceView,该多边形可以在触摸屏幕时旋转。我的应用程序的主要布局是 FrameLayout。 frameLayout
我对 EGL 感到困惑。 我的 GLSurfaceView 创建了一个 EGLContext。现在我创建一个共享上下文。现在我需要使用 EGLExtension。 我必须使用的方法被称为(>=API1
是否可以在 GLSurfaceView 上添加 LinearLayout?如何在 GLSurfaceView 上叠加 LinearLayout?也许 GLSurfaceView 是 Game play
请,有人可以提供使用带有 GLSurfaceView 的 CameraX 库的好例子,找不到任何信息。 最佳答案 不存在将 CameraX 与自定义表面/TextureView 一起使用的文档。在 C
我尝试在我的 GLSurfaceView 上使用 setRotation(),但 View 似乎没有围绕屏幕中心的枢轴旋转。代码如下: class MyGLSurfaceView extends GL
我正在尝试运行 android 示例 http://developer.android.com/training/graphics/opengl/index.html 我必须向 View 添加一些控件
我有 GLSurfaceView 和顶部带有按钮的小型 LinearLayout,代表菜单。此布局默认隐藏,并在用户点击某个按钮时从屏幕底部(通过 TranslateAnimation)显示。 (也尝
我在我的应用程序中使用开放式 GL。我还在我的 Activity 中创建了一个操作栏,将其 View 设置为表面 View 。但是我无法获得操作栏。它在我的表面后面吗?我不知道。我只是无法得到它。我当
实际上,我有一个项目正在使用 GLSurfaceView。此时此组件作为主控件放置在整个屏幕上。在此配置中,我能够正确处理绘图功能。 在下一步中,我想更改 GLSurfaceView 的大小和位置。我
根据标题,从暂停状态恢复后 GLSurfaceView 为空白。 Renderer的onSurfaceCreated、onSurfaceChanged和onDrawFrame在恢复后被调用,但是屏幕还
我有一个带有可绘制对象作为背景的 GLSurfaceView,但是在没有 surfaceView.setZOrderOnTop(true) 的情况下渲染时只有背景可见 我需要避免使用 setZOrde
我正在尝试在 Android 上使用 GLSurfaceView 并遇到问题。我正在使用 this OpenGL article 中的代码.它运行良好,但当我旋转设备时,我注意到分配的内存在增长。 所
我有一个 GLSurfaceView,我在其中使用 OpenGL 显示一些动画。 我现在想给这个 View 添加一个按钮。这是如何实现的? 不涉及xml布局能不能做到? 最佳答案 您可以手动构建 Vi
我正在做一个项目,我们需要记录渲染的 OpenGL 表面。 (例如,如果我们使用 GLsurfaceView,我们需要记录表面以及来自 MIC 的音频) 目前我通过将视频源设置为 VIDEO_SOUR
我尝试使用 GLSurfaceView 进行渲染,并通过 docs我设置格式: getHolder().setFormat(PixelFormat.TRANSLUCENT); 我使用的是 GLSurf
我的程序在两个 Activity 之间切换,每个 Activity 都会膨胀一个使用 VBO 的派生 GLSurfaceView。 在两个Activity之间来回切换几次后,程序崩溃并抛出如下异常:
我正在尝试在 android 上开始使用 opengl。 我找到的所有教程都假定 GLSurfaceView 可用。当我尝试编译我的项目时,我得到了 GLSurfaceView cannot be r
我目前正在开发一个 Android 应用程序,我希望在单击按钮时重新启动 glSurfaceView。即当单击按钮时,应再次将图像读入程序并重置所有纹理。我应该怎么办?谢谢。 最佳答案 听起来您只想在
虽然我发现了一个关于 Sharing the GLES20 context... 的问题,我觉得我的情况比较宽泛一些。 我有两个 GLSurfaceView 在 FrameLayout 中彼此分层。
我正在尝试使用 MediaCodec 播放视频并通过 GLSurfaceView 呈现它,以便我可以进行帧回调。 package com.alwaysinnovating.aimediacodec;
我是一名优秀的程序员,十分优秀!