Hallo allemaal,
Ik heb een GLSurfaceView opgenomen in een layout xml bestand en nu wil ik bepaalde functionaliteit aansturen door middel van knoppen. Ik heb de kubus van les 7 uit deze pagina opgenomen in de view. Deze kun je besturen door de kubus met je vinger te draaien. Aan de bovenkant van het scherm kun je de kubus schalen. Dit schalen wil ik door middel van knoppen doen.
de layout:
de activity class:HTML-code:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent"> <!-- Here is where we put the SurfaceView, in a frame so that we can stack other views on top of it. --> <FrameLayout Android:layout_width="fill_parent" Android:layout_height="0px" Android:layout_weight="1"> <com.nea.nehe.lesson07.Lesson07 Android:id="@+id/glsurfaceview" Android:layout_width="fill_parent" Android:layout_height="wrap_content" /> <LinearLayout Android:id="@+id/hidecontainer" Android:orientation="vertical" Android:visibility="gone" Android:gravity="center" Android:layout_width="fill_parent" Android:layout_height="fill_parent"> <Button Android:id="@+id/hideme1" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:layout_gravity="center" Android:visibility="gone" Android:text="hide me"/> <Button Android:id="@+id/hideme2" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:layout_gravity="center" Android:visibility="gone" Android:text="hide me"/> </LinearLayout> </FrameLayout> <LinearLayout Android:orientation="horizontal" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:layout_gravity="center"> <Button Android:id="@+id/plus" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="+"/> <Button Android:id="@+id/min" Android:layout_width="wrap_content" Android:layout_height="wrap_content" Android:text="-"/> </LinearLayout> </LinearLayout>
de GLSurfaceView:Code:import Android.app.Activity; import Android.os.Bundle; public class Run extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout); } /** * Remember to resume our Lesson */ @Override protected void onResume() { super.onResume(); } /** * Also pause our Lesson */ @Override protected void onPause() { super.onPause(); } }
Dit stukje draait het eigenlijk om die wil ik kunnen bedienen met knoppen.Code:import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import Android.content.Context; import Android.opengl.GLSurfaceView; import Android.opengl.GLU; import Android.opengl.GLSurfaceView.Renderer; import Android.util.AttributeSet; import Android.view.KeyEvent; import Android.view.MotionEvent; public class Lesson07 extends GLSurfaceView implements Renderer { /** Cube instance */ private Cube cube; /* Rotation values */ private float xrot; //X Rotation private float yrot; //Y Rotation /* Rotation speed values */ private float xspeed; //X Rotation Speed ( NEW ) private float yspeed; //Y Rotation Speed ( NEW ) private float z = -5.0f; //Depth Into The Screen ( NEW ) private int filter = 0; //Which texture filter? ( NEW ) /* * These variables store the previous X and Y * values as well as a fix touch scale factor. * These are necessary for the rotation transformation * added to this lesson, based on the screen touches. ( NEW ) */ private float oldX; private float oldY; private final float TOUCH_SCALE = 0.2f; //Proved to be good for normal rotation ( NEW ) /** The Activity Context */ private Context context; public Lesson07(Context context, AttributeSet attr) { super(context, attr); //Set this as Renderer this.setRenderer(this); //Request focus, otherwise buttons won't react this.requestFocus(); this.setFocusableInTouchMode(true); this.context = context; cube = new Cube(); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { //Settings gl.glDisable(GL10.GL_DITHER); //Disable dithering ( NEW ) gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background gl.glClearDepthf(1.0f); //Depth Buffer Setup gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } /** * Here we do our drawing */ public void onDrawFrame(GL10 gl) { //Clear Screen And Depth Buffer gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); //Reset The Current Modelview Matrix gl.glTranslatef(0.0f, 0.0f, z); //Move z units into the screen gl.glScalef(0.8f, 0.8f, 0.8f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen //Rotate around the axis based on the rotation matrix (rotation, x, y, z) gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y cube.draw(gl, filter); //Draw the Cube //Change rotation factors xrot += xspeed; yrot += yspeed; } /** * If the surface changes, reset the view */ public void onSurfaceChanged(GL10 gl, int width, int height) { if(height == 0) { //Prevent A Divide By Zero By height = 1; //Making Height Equal One } gl.glViewport(0, 0, width, height); //Reset The Current Viewport gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix gl.glLoadIdentity(); //Reset The Projection Matrix //Calculate The Aspect Ratio Of The Window GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix gl.glLoadIdentity(); //Reset The Modelview Matrix } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { // if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { yspeed -= 0.1f; } else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { yspeed += 0.1f; } else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) { xspeed -= 0.1f; } else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { xspeed += 0.1f; } else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { filter += 1; if(filter > 2) { filter = 0; } } //We handled the event return true; } @Override public boolean onTouchEvent(MotionEvent event) { // float x = event.getX(); float y = event.getY(); //If a touch is moved on the screen if(event.getAction() == MotionEvent.ACTION_MOVE) { //Calculate the change float dx = x - oldX; float dy = y - oldY; //Define an upper area of 10% on the screen int upperArea = this.getHeight() / 10; //Zoom in/out if the touch move has been made in the upper if(y < upperArea) { z -= dx * TOUCH_SCALE / 2; //Rotate around the axis otherwise } else { xrot += dy * TOUCH_SCALE; yrot += dx * TOUCH_SCALE; } } //Remember the values oldX = x; oldY = y; //We handled the event return true; } }
Iemand enig idee hoe ik dat zou moeten doen?Code://Define an upper area of 10% on the screen int upperArea = this.getHeight() / 10; //Zoom in/out if the touch move has been made in the upper if(y < upperArea) { z -= dx * TOUCH_SCALE / 2;
Vast bedankt!



LinkBack URL
About LinkBacks


Met citaat reageren