Android Official Development Document Training Series Course Chinese version: OpenGL Drawing Response Touch Event

Source: Internet
Author: User

Original address: http://android.xsoftlab.net/training/graphics/opengl/touch.html

Making the graph rotate according to the program design is still not able to play its rightful part in OpenGL. But if the user can directly control the rotation of the graph, this is the true purpose of OpenGL. The real key is to enable the program to touch interactively. This is mainly by rewriting Glsurfaceview's Ontouchevent () method to achieve touch event monitoring.

This lesson will show you how to listen for touch events to enable the user to rotate the graphic.

Setting up a touch listener

To enable OpenGL to listen for touch events, you must override the Ontouchevent () method in the Glsurfaceview class. The following implementation shows how to listen for the Motionevent.action_move event and how to make the event drive the rotation of the graph.

Private Final floatTouch_scale_factor =180.0F/ the;Private floatMpreviousx;Private floatMpreviousy;@Override Public Boolean ontouchevent(Motionevent e) {//motionevent reports input details from the touch screen    //and other input controls. In this case, you is only    //Interested in events where the touch position changed.    floatx = E.getx ();floaty = e.gety ();Switch(E.getaction ()) { CaseMotionevent.action_move:floatDX = X-mpreviousx;floatdy = 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.setangle (Mrenderer.getangle () + (dx + dy) * TOUCH_SCALE_FAC            TOR));    Requestrender ();    } mpreviousx = x; Mpreviousy = y;return true;}

It is important to note that after calculating the rotation angle, this method calls the Requestrender () method, which notifies the renderer that it can render. This method is best suited for this place because the frame does not need to be redrawn until it has changed in perspective. In any case, this approach does not have any effect on efficiency unless you also set a request to redraw when the data changes. This request is set by the Setrendermode () method, so make sure that the following line of code is not commented:

publicMyGLSurfaceView(Context context) {    ...    // Render the view only when there is a change in the drawing data    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);}
Exposure rotation angle

The example code in the

above would require an exposed member method to expose the angle of rotation. Once the render code is running in a child thread, the public member must be declared volatile. The following code declares the volatile property and exposes its Get,set method:

publicclass MyGLRenderer implements GLSurfaceView.Renderer {    ...    publicvolatilefloat mAngle;    publicfloatgetAngle() {        return mAngle;    }    publicvoidsetAngle(float angle) {        mAngle = angle;    }}
Request rotation

To touch event-driven rotation, you need to annotate the code that generates the angle, and then add the Mangle member property, which contains the angle generated by the touch event mangle:

 Public void Ondrawframe(GL10 GL) {    ...float[] Scratch =New float[ -];//Create a rotation for the triangle    //long time = Systemclock.uptimemillis ()% 4000L;    //Float angle = 0.090f * ((int) time);Matrix.setrotatem (Mrotationmatrix,0, MAngle,0,0, -1.0f);//Combine the rotation matrix with the projection and camera view    //Note that the Mmvpmatrix factor *must is first* in order    //For the matrix multiplication product to be correct.Matrix.multiplymm (Scratch,0, Mmvpmatrix,0, Mrotationmatrix,0);//Draw triangleMtriangle.draw (scratch);}

If you have completed the steps described above, start the program and then drag on the screen to rotate the triangle:

Android Official Development Document Training Series Course Chinese version: OpenGL Drawing Response Touch Event

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.