gpt4 book ai didi

Android OpenGL 传递 OnTouch 事件值

转载 作者:行者123 更新时间:2023-11-30 01:53:33 25 4
gpt4 key购买 nike

我有问题。我不知道如何将 OnTouch 方法中的值 x 和 y 传递给私有(private)最终类 MyRenderer 并更新 OnDrawFrame tex.draw(,x,y, ,,) 函数 x 和 y 值能够平移我想用手指移动的对象。

不胜感激~

public class Stage extends GLSurfaceView{

// Stage width and height
private float w, h;

// Screen width and height
private int screenWidth, screenHeight;

// Our native vertex buffer
private FloatBuffer vertexBuffer;

private Texture tex;

@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
float x, y;
int pointerIndex;
int pointerId;

if (action == MotionEvent.ACTION_DOWN) {
pointerId = event.getPointerId(0);
x = event.getX();
y = event.getY();
pointerIndex = 0;
} else {
pointerIndex = event.getActionIndex();
pointerId = event.getPointerId(pointerIndex);
x = event.getX(pointerIndex);
y = event.getY(pointerIndex);
}
Log.v("this X", String.valueOf(x));
Log.v("this Y", String.valueOf(y));

return true;
}

public Stage(Context context, AttributeSet attrs) {
super(context, attrs);
setEGLConfigChooser(8, 8, 8, 8, 0, 0);
setRenderer(new MyRenderer());
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
float vertices[] = {
-0.5f, -0.5f, 0.0f, // 0. left-bottom
0.5f, -0.5f, 0.0f, // 1. right-bottom
-0.5f, 0.5f, 0.0f, // 2. left-top
0.5f, 0.5f, 0.0f // 3. right-top
};

ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);

tex = new Texture(R.drawable.kdk);

}

private final class MyRenderer implements GLSurfaceView.Renderer {

public final void onDrawFrame(GL10 gl) {

gl.glClear(GLES10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
tex.prepare(gl, GL10.GL_CLAMP_TO_EDGE);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
tex.draw(gl, w / 2, h / 2, tex.getWidth(), tex.getHeight(), 0);

}

public final void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glClearColor(0, 0, 0, 0);

if(width > height) {
h = 600;
w = width * h / height;
} else {
w = 600;
h = height * w / width;
}
screenWidth = width;
screenHeight = height;

gl.glViewport(0, 0, screenWidth, screenHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0, w, h, 0, -1, 1);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}

public final void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set up alpha blending
gl.glEnable(GL10.GL_ALPHA_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);

// We are in 2D. Why needs depth?
gl.glDisable(GL10.GL_DEPTH_TEST);

// Enable vertex arrays (we'll use them to draw primitives).
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

// Enable texture coordination arrays.
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

tex.load(getContext());
}

}

}

其他编码

How to apply drag drop and scale in Android OpenGL ES

最佳答案

为此,您必须将 MyRenderer 属性添加到您的 Stage 类。

此解决方案可行,但可能不是最好的,因此请随意发表评论。


  1. 您需要通过删除 final 语句来更改内部类 MyRenderer 的定义:

    EDIT :我不确定这个,因为 final class 会将其所有方法设置为 final 但可能不是属性。如果有人能回答,我会很乐意阅读。

    private final class MyRenderer implements GLSurfaceView.Renderer

    private class MyRenderer implements GLSurfaceView.Renderer


  1. 在您的舞台类(class)中:

    public class Stage extends GLSurfaceView {

    // Stage width and height
    private float w, h;

    ...

    // The renderer
    MyRenderer mRenderer;

    ...

    public Stage(Context context, AttributeSet attrs) {

    mRenderer = new MyRenderer();

    super(context, attrs);
    setEGLConfigChooser(8, 8, 8, 8, 0, 0);

    setRenderer(mRenderer);

    ...
    }
    }

  1. 将 setter 添加到您的 MyRenderer 类:

    private class MyRenderer implements GLSurfaceView.Renderer {

    float xPos;
    float yPos;

    // ...

    public void setX(float x) {
    xPos = x;
    }

    public void setY(float y) {
    yPos = y;
    }

    public void setXY(float x, float y) {
    xPos = x;
    yPos = y;
    }
    }

  1. 最后在 OnTouch 方法中设置这些值。

编辑:

设置 xPos 和 yPos 默认值:

private class MyRenderer implements GLSurfaceView.Renderer {

float xPos;
float yPos;

boolean initialised = false;

...

public final void onSurfaceChanged(GL10 gl, int width, int height) {

if (!initialised) {
xPos = width /2;
yPos = height / 2;
initialised = true;
}

...
}

...
}

编辑 2:

为避免竞争条件,请使用一个函数同时设置 X 和 Y 值。进入该方法时,它将使用传入参数的值创建临时变量。这将确保传递的 x 和 y 值来自同一事件。

private class MyRenderer implements GLSurfaceView.Renderer {

float xPos;
float yPos;

// ...


public void setXY(float x, float y) {
xPos = x;
yPos = y;
}
}

关于Android OpenGL 传递 OnTouch 事件值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32625793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com