XML布局中的Android GLSurfaceView

概述我试图将GLSurfaceView和一个按钮放入相同的xml布局中.但是我的应用程序在我的设备上运行时会自动关闭.有人可以帮助我,告诉我我错过了什么或我的代码有什么问题?这是我的代码===================<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="ht

我试图将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全部内容,希望文章能够帮你解决XML布局中的Android GLSurfaceView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1110389.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存