gpt4 book ai didi

android - 带四边形的手指画 : how to avoid strange blinking

转载 作者:行者123 更新时间:2023-11-30 04:12:51 31 4
gpt4 key购买 nike

我正在尝试通过 GLSurfaceView 实现手指绘画应用。

但四边形闪烁太多,我不得不重新绘制它们几次(请参阅 PaintGL 类中的 onTouchEvent)。看来我正在使用不同的帧缓冲区,但我不知道如何在它们之间切换或禁用它们以避免这种讨厌的闪烁。

这是我的渲染器:

public class PaintRenderer implements GLSurfaceView.Renderer 
{

private FloatBuffer vertexBuffer;
public ArrayList<HardPoint> l;
public boolean isPressed = false;

public PaintRenderer()
{
super ();
l = new ArrayList<HardPoint>();
}

public void onDrawFrame(GL10 gl)
{

ArrayList<HardPoint> n;
int count;

synchronized (l)
{
n = (ArrayList<HardPoint>) l.clone();
}

if (n.size() == 0)
return;

n.add(n.get(n.size() - 1));

for (int i = 1; i < n.size(); i++) {

HardPoint start = n.get(i-1);
HardPoint end = n.get(i);

if (!end.isPressed)
continue;

float dx = end.x - start.x;
float dy = end.y - start.y;

count = (int) Math.ceil(Math.max(Math.sqrt(dx * dx + dy * dy), 1));

for (int j = 0; j < count; j++)
{
drawQuad(gl, new PointF(start.x + dx * (float)j/count , start.y + dy * (float)j/count), 40.0f);
}

}

}

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

gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, -1.0f, 1.0f);

gl.glMatrixMode(GL10.GL_MODELVIEW);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_DEPTH_TEST);

}

public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
}

public void drawQuad(GL10 gl_main, PointF center, float side)
{
int VERTEX_COUNT = 4;
float vertices[] =
{
// Вершины квадрата
center.x - side/2, center.y + side/2, // 0. левая нижняя
center.x + side/2, center.y + side/2, // 1. правая нижняя
center.x - side/2, center.y - side/2, // 2. левая верхняя
center.x + side/2, center.y - side/2, // 3. правая верхняя
};

ByteBuffer vbb = ByteBuffer.allocateDirect(VERTEX_COUNT * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();

vertexBuffer.put(vertices);

vertexBuffer.position(0);

gl_main.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl_main.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);

gl_main.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

}

}

这是我的 GLSurfaceView 类:

public class PaintGL extends GLSurfaceView
{

private Context mContext;
private PaintRenderer renderer;
private PointF prev_loc, cur_loc;

public PaintGL(Context context) {
super(context);
mContext = context;
renderer = new PaintRenderer();
setRenderer(renderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
}


@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized (renderer.l)
{
renderer.l.add(0, new HardPoint(event.getX(), this.getHeight() - event.getY(), true));
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_UP)
{
//FIXME Мерцание первого quad'a при достаточно большом количестве точек
int count = 10;
synchronized (renderer.l) {
renderer.l.add(0, new HardPoint(event.getX(), this.getHeight() - event.getY(),
event.getAction() == MotionEvent.ACTION_UP ? false : true));
while (renderer.l.size() > count)
{
renderer.l.remove(count);
}
}
requestRender();
Log.d("x", renderer.l.size()+" ");
}

return true;
}

}

和 HardPoint 类:

public class HardPoint extends PointF 
{
boolean isPressed;

public HardPoint()
{
isPressed = false;
}

public HardPoint (float x, float y, boolean pressed)
{
super(x, y);
isPressed = pressed;
}

}

最佳答案

几点:

There are two basic rules for writing efficient code:

  • Don't do work that you don't need to do.
  • Don't allocate memory if you can avoid it.
  • ArrayList.clone创建一个浅拷贝。您必须为深拷贝添加代码或使用像 cloning 这样的库.

  • 摆脱单列表上 UI- 和 Renderthread 之间的同步。使用第二个列表,其中 UI-Thread 仅添加 HardPoint 实例(同步)。在onDrawFrame 开始时,将HardPoint 实例传输到“render”列表(同步)。然后,您可以限制列表的大小。这消除了克隆

  • 与其每次都重新创建四边形几何体,不如创建一次,然后将其平移到正确的位置。

  • 要消除 HardPoint 实例的创建,您可以应用池 (here's the AndEngine one) 预分配实例。

关于android - 带四边形的手指画 : how to avoid strange blinking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10547044/

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