Rob-Hoo 2014-11-24 10:19 采纳率: 0%
浏览 2425

Android一个动画,连续运行80个小时之后会停止,没发现有异常log,应用也没有死掉。

Android一个动画,连续运行80个小时之后会停止,没发现有异常log,应用也没有死掉。
动画是一个六面正方体来回旋转,开始正常旋转,但是在连续旋转80个小时之后,正方体就会停止旋转,静止在一个状态,但是应用程序并没有死掉,可以正常退出,并且没有异常log,代码如下:
代码在楼下的回答里边,这个问答加代码总是格式不正确。

  • 写回答

1条回答 默认 最新

  • Rob-Hoo 2014-11-24 10:19
    关注
    public class CuteSurfaceView extends GLSurfaceView {
    public float rotateX; // 用于正方体x轴的旋转;
    public float rotateY; // 用于正方体y轴的旋转;
    public float rotateZ; // 用于正方体z轴的旋转;
    private boolean bAutoRotate = true;
    public CuteSurfaceView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setRenderer(new GuteRender());      
    }
    private class GuteRender implements Renderer {
        private int one = 0x10000;
        private int[] quarter = { -one, -one, one, one, -one, one, one, one,
                one, -one, one, one,
    
                -one, -one, -one, -one, one, -one, one, one, -one, one, -one,
                -one,
                -one, one, -one, -one, one, one, one, one, one, one, one, -one,
                -one, -one, -one, one, -one, -one, one, -one, one, -one, -one,
                one,
                one, -one, -one, one, one, -one, one, one, one, one, -one, one,
                -one, -one, -one, -one, -one, one, -one, one, one, -one, one,
                -one, };
        private int[] texCoords = { one, 0, 0, 
                0,  0, one, 
                one, one,0, 
                0, 0,one,               
                one, one, one, 
                0,one, one, 
                one, 0, 0, 
                0, 0, one,              
                0, one, one, 
                one,one, 0, 
                0, 0, 0, 
                0, 0, one,              
                one, one, one, 0, 
                one, 0, 0, 0,
                0, one, one, one, };
        // 三角形索引数据
        ByteBuffer indices1 = ByteBuffer.wrap(new byte[] { 0, 1, 3, 2, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, });
        ByteBuffer indices2 = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 4, 5, 7,
                6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, });
        ByteBuffer indices3 = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0,
                0, 8, 9, 11, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, });
        ByteBuffer indices4 = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 12, 13, 15, 14, 0, 0, 0, 0, 0, 0, 0, 0, });
        ByteBuffer indices5 = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 17, 19, 18, 0, 0, 0, 0, });
        ByteBuffer indices6 = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 23, 22, });
        Bitmap[] bmp = new Bitmap[6];
        int[] tex_id = new int[6];
        public GuteRender() {
            bmp[0] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img1);
            bmp[1] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img2);
            bmp[2] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img3);
            bmp[3] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img4);
            bmp[4] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img5);
            bmp[5] = BitmapFactory.decodeResource(getResources(),
                    R.drawable.img6);
            setFocusable(true);
        }
        @Override
        public void onDrawFrame(GL10 gl) {
            // TODO Auto-generated method stub
            // 清楚屏幕和深度缓存
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            // 重置当前的观察模型矩阵
            gl.glLoadIdentity();
            // 现将屏幕向里移动,用来画正方体
            gl.glTranslatef(0.0f, 0.0f, -6.0f);
            if (bAutoRotate) {
    //              rotateX += 0.5;
    //              rotateY += 0.6;
    //              // rotateZ+=0.3;                
                rotateX += 0.5;
                rotateY += 0.5;
    //              rotateZ +=10.5;             
            }
            // 设置3个方向的旋转
            gl.glRotatef(rotateX*10, 1.0f, 0.0f, 0.0f);
            gl.glRotatef(rotateY*10, 0.0f, 1.0f, 0.0f);
            gl.glRotatef(rotateZ*10, 0.0f, 0.0f, 1.0f);
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            // 纹理的使用与开启颜色渲染一样,需要开启纹理功能
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            // 设置正方体 各顶点
            gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferUtil.iBuffer(quarter));
            gl.glTexCoordPointer(2, GL10.GL_FIXED, 0,BufferUtil.iBuffer(texCoords));
            // 绘制立方体并绑定纹理
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[0]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
                    indices1);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[1]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 8, GL10.GL_UNSIGNED_BYTE,
                    indices2);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[2]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 12,
                    GL10.GL_UNSIGNED_BYTE, indices3);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[3]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 16,
                    GL10.GL_UNSIGNED_BYTE, indices4);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[4]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 20,
                    GL10.GL_UNSIGNED_BYTE, indices5);
            gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[5]);
            gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24,
                    GL10.GL_UNSIGNED_BYTE, indices6);
    
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        }
        // 当窗口改变时,调用,至少在创建窗口时调用一次,这边设置下场景大小
        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            // TODO Auto-generated method stub
            // 设置OpenGL场景大小
            float ratio = (float) width / height;
            gl.glViewport(0, 0, width, height);
            gl.glMatrixMode(GL10.GL_PROJECTION);// 设置为投影矩阵模式
            gl.glLoadIdentity();// 重置
            gl.glFrustumf(-ratio, ratio, -1, 1, 2, 10);// 设置视角
            gl.glMatrixMode(GL10.GL_MODELVIEW);
            gl.glLoadIdentity();
        }
        // 当窗口被创建时我们可以做些初始化工作
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            // TODO Auto-generated method stub          
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
            // 设置清除屏幕时所用的颜色,参数依次为红、绿、蓝、Alpha值
            // gl.glClearColor(0, 0, 0, 0);
            gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
            gl.glEnable(GL10.GL_CULL_FACE);
            // 启用阴影平滑
            gl.glShadeModel(GL10.GL_SMOOTH);
            gl.glEnable(GL10.GL_DEPTH_TEST);// 启用深度测试
            // 以下是关于深度缓存的设置,非常重要
            gl.glClearDepthf(1.0f);// 设置深度缓存
            gl.glDepthFunc(GL10.GL_LEQUAL);// 所做深度测试的类型
            // 告诉系统对透视进行修正
            gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
            // 允许2D贴图
            gl.glEnable(GL10.GL_TEXTURE_2D);
            // gl.glGenTextures(6, tex_id, 0);
            IntBuffer textureBuffer = IntBuffer.allocate(6);
            gl.glGenTextures(6, textureBuffer);
            tex_id = textureBuffer.array();
            for (int i = 0; i < 6; i++) {
                gl.glBindTexture(GL10.GL_TEXTURE_2D, tex_id[i]);
                GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp[i], 0);
                gl.glTexParameterx(GL10.GL_TEXTURE_2D,
                        GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
                gl.glTexParameterx(GL10.GL_TEXTURE_2D,
                        GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
            }           
             if(flag == 1)
                {
                 rotateX += 2.0f;
                }else if(flag == 2)
                {
                    rotateX -= 2.0f;
                }else if(flag == 3)
                {
                    rotateY += 2.0f;
                }else if(flag == 4)
                {
                    rotateY -= 2.0f;
                }
                if(flag == 1 || flag == 2)
                if((int)(rotateX%90)==0)
                {
                    flag = 0;
                }
                if(flag == 3 || flag == 4)
                if((int)(rotateY%90) ==0)
                {
                    flag =0;
                }
        }
    }
    public static class BufferUtil {
        public static IntBuffer intBuffer;
        public static IntBuffer iBuffer(int[] a) {
            // 先初始化buffer,数组的长度*4,因为一个float占4个字节
            ByteBuffer mbb = ByteBuffer.allocateDirect(a.length * 4);
            // 数组排列用nativeOrder
            mbb.order(ByteOrder.nativeOrder());
            intBuffer = mbb.asIntBuffer();
            intBuffer.put(a);
            intBuffer.position(0);
            return intBuffer;
        }
    }
    int flag=0;
    int tag=0;
    float last_x;
    float last_y;
    float flagx;
    float flagy;
    long mm;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大