在Android的旋转顺时针和逆时针方 [英] In android to rotate a square in clockwise and anticlockwise

查看:264
本文介绍了在Android的旋转顺时针和逆时针方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Android的概念OPENGL创建一些简单的动​​画和我有什么做的是在这两个时钟旋转一个正方形和anticlock明智direction.i意味着某一特定时间的平方将在顺时针旋转,分别anticlock明智的。 。感谢advacne :)。和对不起我的英语:(

 我的主要活动 公共类主要活动扩展{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    GLSurfaceView视图=新GLSurfaceView(本);
    view.setRenderer(新OpenGLRenderer());
    的setContentView(视图);
}

}

OpenGLRenderer.java

 公共类OpenGLRenderer实现渲染{
私人方方;
私人浮动角= 0;公共OpenGLRenderer(){
    //初始化我们的广场。
    方=新广场();
}公共无效onSurfaceCreated(GL10 GL,EGLConfig配置){
    //设置背景色为黑色(RGBA)。
    gl.glClearColor(0.0,0.0,0.0,0.5F);
    //启用光滑着色,默认不是真的需要。
    gl.glShadeModel(GL10.GL_SMOOTH);
    //深度缓存设置。
    gl.glClearDepthf(1.0F);
    //启用深度测试。
    gl.glEnable(GL10.GL_DEPTH_TEST);
    //类型深度测试要做。
    gl.glDepthFunc(GL10.GL_LEQUAL);
    //非常好的角度计算。
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);
}公共无效onDrawFrame(GL10 GL){
    //清除屏幕和深度缓存。
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    //替换为单位矩阵当前矩阵
    gl.glLoadIdentity();
    //平移10个单位到屏幕。
    gl.glTranslatef(0,0,-10);    // SQUARE
    //保存当前的矩阵。
    gl.glPushMatrix();
    //逆时针旋转广场。
    gl.glRotatef(角度,0,0,1);
    //绘制方形。
    square.draw(GL);
    //恢复的最后一个矩阵。
    gl.glPopMatrix();
    square.draw(GL);    //恢复为基质,因为它之前B.
    gl.glPopMatrix();    //可适当增加的角度。
    角度++;
}公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT){
    //将当前视图移植到新的大小。
    gl.glViewport(0,0,宽度,高度);
    //选择投影矩阵
    gl.glMatrixMode(GL10.GL_PROJECTION);
    //重置投影矩阵
    gl.glLoadIdentity();
    //计算窗口的高宽比
    GLU.gluPerspective(GL,45.0f,(浮点)宽/(浮点)的高度,0.1F,
            100.0f);
    //选择模型视图矩阵
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    //重置模型视点矩阵
    gl.glLoadIdentity();
}

}

Squar.java

 公共类广场{
//我们的顶点。
私人浮动顶点[] = {
          -1.0F,1.0F,0.0,// 0,左上
          -1.0F,-1.0F,0.0,// 1,左下
           1.0F,-1.0F,0.0,// 2,右下
           1.0F,1.0F,0.0,// 3,右上
    };//我们想连接它们的顺序。
私人短[]索引= {0,1,2,0,2,3};//我们的顶点缓冲。
私人FloatBuffer vertexBuffer;//我们的索引缓冲区。
私人ShortBuffer indexBuffer;市民广场(){
    //一个浮点数为4个字节,因此,我们如果乘以人数
    //有4个顶点。
    ByteBuffer的VBB = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(顶点);
    vertexBuffer.position(0);    //短则2个字节,因此,我们如果乘以人数
    //用2个顶点。
    IBB的ByteBuffer = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(指数);
    indexBuffer.position(0);
}/ **
 *此功能引起了我们的屏幕上万。
 * @参数GL
 * /
公共无效画(GL10 GL){
    //逆时针绕。
    gl.glFrontFace(GL10.GL_CCW);
    //启用剔除脸。
    gl.glEnable(GL10.GL_CULL_FACE);
    //什么面临着脸上剔除删除。
    gl.glCullFace(GL10.GL_BACK);    //启用顶点缓冲器写入和过程中使用的
    //渲染。
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    //指定顶点的阵列的位置和数据格式
    //坐标绘制时使用。
    gl.glVertexPointer(3,GL10.GL_FLOAT,0,vertexBuffer);    gl.glDrawElements(GL10.GL_TRIANGLES,indices.length,
            GL10.GL_UNSIGNED_SHORT,indexBuffer);    //禁用顶点缓冲区。
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    //禁用脸部剔除。
    gl.glDisable(GL10.GL_CULL_FACE);
}

}

和我在这里code对该部分旋转两个side..now问题只有

 公共类OpenGLRenderer实现渲染{
私人方方;
私人浮动角= 0;公共OpenGLRenderer(){
    方=新广场();
}
公共无效onSurfaceCreated(最终GL10 GL,EGLConfig配置){
    gl.glClearColor(0.0,0.0,0.0,0.5F);
    gl.glShadeModel(GL10.GL_SMOOTH_LINE_WIDTH_RANGE);
    gl.glClearDepthf(1.0F);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,GL10.GL_NICEST);
}公共无效onDrawFrame(最终GL10 GL){     线程t =新主题(新的Runnable(){            公共无效的run(){                        尝试{                            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                            gl.glLoadIdentity();
                            gl.glTranslatef(0.0,4.9f,-9.3f);
                            //gl.glTranslatef(2.0f,0.0,0.0);
                            gl.glPushMatrix();
                            gl.glRotatef(利用广角,0,0,1);                            gl.glScalef(1.0F,1.0F,1.0F);
                            square.draw(GL);
                            gl.glPopMatrix();
                            角度++;
                              视频下载(5000);                        }
                        赶上(InterruptedException的E){
                        }
                       // 去做
                        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                        gl.glLoadIdentity();
                        gl.glTranslatef(0.0,4.9f,-9.3f);
                        //gl.glTranslatef(2.0f,0.0,0.0);
                        gl.glPushMatrix();
                        gl.glRotatef(角度,0,0,1);                        //gl.glScalef(1.0f,1.0F,1.0F);
                        square.draw(GL);
                        gl.glPopMatrix();
                        角度++;
            }
       });
        t.start();    }公共无效onSurfaceChanged(GL10 GL,诠释的宽度,高度INT){
    gl.glViewport(0,0,宽度,高度);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    //glClearColor(5.0f,6.0f,0.0,5.5F);黄色
    gl.glLoadIdentity();
    //GLU.gluPerspective(gl,45.0f,(浮点)宽/(浮点)的高度,0.1F,100.0f);
    GLU.gluPerspective(GL,80.0f,(浮点)宽/(浮点)的高度,8.0f,10.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

}


解决方案

 公共类主要活动扩展{
私人Runnable接口可运行;@覆盖公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CONTEXT_MENU);
    getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    最后GLSurfaceView视图=新GLSurfaceView(本);
    view.setRenderer(新OpenGLRenderer());
    的setContentView(视图);    可运行=新的Runnable(){        @覆盖
        公共无效的run(){
            // TODO自动生成方法存根
            尝试
            {
                view.setRenderer(新OpenGLRenderer());
                视频下载(10);
            完();            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
        }
    };
    尝试
    {
        线程t =新主题(NULL,可运行);
        t.start();
        view.setRenderer(新OpenGLRenderer1());
       //意图I =新意图(这一点,OpenGLRenderer.class);
         // startActivity(ⅰ);
    }
    赶上(例外五)
    {    }        }
}

ü可以尝试一下。

I created some simple animation in android using OPENGL concept and what i what to do is to rotate a square in both clock and anticlock wise direction.i mean for a particular time the square will rotate in clock wise and anticlock wise respectively.. thanks in advacne :). and sorry for my english :(.

    my main activity



 public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    GLSurfaceView view = new GLSurfaceView(this);
    view.setRenderer(new OpenGLRenderer());
    setContentView(view);
}

}

OpenGLRenderer.java

public class OpenGLRenderer implements Renderer {
private Square square;
private float angle = 0;

public OpenGLRenderer() {
    // Initialize our square. 
    square = new Square();
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    // Set the background color to black ( rgba ).
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    // Enable Smooth Shading, default not really needed.
    gl.glShadeModel(GL10.GL_SMOOTH);
    // Depth buffer setup.
    gl.glClearDepthf(1.0f);
    // Enables depth testing.
    gl.glEnable(GL10.GL_DEPTH_TEST);
    // The type of depth testing to do.
    gl.glDepthFunc(GL10.GL_LEQUAL);
    // Really nice perspective calculations.
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public void onDrawFrame(GL10 gl) {
    // Clears the screen and depth buffer.
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    // Replace the current matrix with the identity matrix
    gl.glLoadIdentity();
    // Translates 10 units into the screen.
    gl.glTranslatef(0, 0, -10); 

    // SQUARE 
    // Save the current matrix.
    gl.glPushMatrix();
    // Rotate square counter-clockwise.
    gl.glRotatef(angle, 0, 0, 1);
    // Draw square .    
    square.draw(gl);
    // Restore the last matrix.
    gl.glPopMatrix();


    square.draw(gl);            

    // Restore to the matrix as it was before B.
    gl.glPopMatrix();

    // Increse the angle.
    angle++;
}

public void onSurfaceChanged(GL10 gl, int width, int height) {
    // Sets the current view port to the new size.
    gl.glViewport(0, 0, width, height);
    // Select the projection matrix
    gl.glMatrixMode(GL10.GL_PROJECTION);
    // Reset the projection matrix
    gl.glLoadIdentity();
    // Calculate the aspect ratio of the window
    GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
            100.0f);
    // Select the modelview matrix
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    // Reset the modelview matrix
    gl.glLoadIdentity();
}

}

Squar.java

public class Square {
// Our vertices.
private float vertices[] = {
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right
    };

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;

public Square() {
    // a float is 4 bytes, therefore we multiply the number if 
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if 
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);
}

/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {
    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW);
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE);
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK);

    // Enabled the vertices buffer for writing and to be used during 
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, 
            GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE);
}

}

and here my code for rotate in both side..now issues in this part only

public class OpenGLRenderer implements Renderer {
private Square square;
private float angle = 0;

public OpenGLRenderer() {
    square = new Square();
}
public void onSurfaceCreated(final GL10 gl, EGLConfig config) {
    gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    gl.glShadeModel(GL10.GL_SMOOTH_LINE_WIDTH_RANGE);
    gl.glClearDepthf(1.0f);
    gl.glEnable(GL10.GL_DEPTH_TEST);
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

public void onDrawFrame(final GL10 gl) {

     Thread t = new Thread(new Runnable() {     

            public void run(){  

                        try{

                            gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                            gl.glLoadIdentity();
                            gl.glTranslatef(0.0f, 4.9f,-9.3f);
                            //gl.glTranslatef(2.0f, 0.0f,0.0f);
                            gl.glPushMatrix();
                            gl.glRotatef(-angle, 0, 0, 1);  

                            gl.glScalef(1.0f, 1.0f, 1.0f);
                            square.draw(gl);
                            gl.glPopMatrix();
                            angle++;
                              Thread.sleep(5000);

                        }
                        catch(InterruptedException e){
                        } 
                       // To do
                        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
                        gl.glLoadIdentity();
                        gl.glTranslatef(0.0f, 4.9f,-9.3f);
                        //gl.glTranslatef(2.0f, 0.0f,0.0f);
                        gl.glPushMatrix();
                        gl.glRotatef(angle, 0, 0, 1);   

                        //gl.glScalef(1.0f, 1.0f, 1.0f);
                        square.draw(gl);    
                        gl.glPopMatrix();
                        angle++;
            }
       });
        t.start();

    }



public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, width, height);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    //glClearColor(5.0f, 6.0f, 0.0f, 5.5f); yellow
    gl.glLoadIdentity();
    //GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f, 100.0f);
    GLU.gluPerspective(gl, 80.0f, (float) width / (float) height, 8.0f, 10.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
}

}

解决方案

public class Main extends Activity {
private Runnable runnable;  

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CONTEXT_MENU); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    final GLSurfaceView view = new GLSurfaceView(this);
    view.setRenderer(new OpenGLRenderer());
    setContentView(view);

    runnable =new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try 
            {  


                view.setRenderer(new OpenGLRenderer());
                Thread.sleep(10);
            finish();

            }  
            catch(Exception e)  
            {  
                e.printStackTrace();  
            }  
        }  
    };  
    try  
    {  
        Thread t=new Thread(null,runnable);  
        t.start();  
        view.setRenderer(new OpenGLRenderer1());
       // Intent i=new Intent(this,OpenGLRenderer.class);  
         //   startActivity(i);  
    }  
    catch(Exception e)  
    {  

    }  

        }




}

u can try it.

这篇关于在Android的旋转顺时针和逆时针方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆