Java 多线程更新surfaceview画布
从事咨询项目。最后一分钟的要求是球在屏幕上弹跳不要问为什么…叹气 总之,这些球是用值分组的。10个球是红色的,值100分。5个球是蓝色的,值50分。5个球是绿色的,值25分。5个球是黄色的,值10分 在这种背景下,我采取的方法是扩展SurfaceView并定义5个线程,每个线程管理一组特定的球 每个线程从SurfaceView接收相同的SurfaceHolder 我之所以选择多线程而不是一个线程,是因为管理屏幕上所有球的性能不是最好的 OpenGL现在不是一个真正的选项 下面是其中一个线程类的示例。当线程运行时,它会创建一定数量的球。每个球都随机创建并添加到列表中Java 多线程更新surfaceview画布,java,android,multithreading,android-canvas,surfaceview,Java,Android,Multithreading,Android Canvas,Surfaceview,从事咨询项目。最后一分钟的要求是球在屏幕上弹跳不要问为什么…叹气 总之,这些球是用值分组的。10个球是红色的,值100分。5个球是蓝色的,值50分。5个球是绿色的,值25分。5个球是黄色的,值10分 在这种背景下,我采取的方法是扩展SurfaceView并定义5个线程,每个线程管理一组特定的球 每个线程从SurfaceView接收相同的SurfaceHolder 我之所以选择多线程而不是一个线程,是因为管理屏幕上所有球的性能不是最好的 OpenGL现在不是一个真正的选项 下面是其中一个线程类的示
public class hundred_balls_thread extends base_balls_thread {
public hundred_balls_thread(SurfaceHolder holder, Context ctext, int radius) {
super(holder, ctext, radius);
}
@Override
public void run() {
int x, y, radius;
while (Calorie_balls.size() <= 21) {
x = 100 + (int) (Math.random() * (mCanvasWidth - 200));
y = 100 + (int) (Math.random() * (mCanvasHeight) - 200);
radius = mRadius;
if ((x - mRadius) < 0) {
x = x + mRadius;
}
if ((x + mRadius) > mCanvasWidth) {
x = x - mRadius;
}
if ((y + mRadius) > mCanvasHeight)
y = y - mRadius;
if ((y - mRadius) < 0)
y = y + mRadius;
calorie_ball ball = new calorie_ball(x, y, radius, context.getResources().getColor(R.color.red100ball), "100");
boolean addit = true;
Calorie_balls.add(ball);
}
super.run();
}
}
下面是它们都扩展的基类:
public class base_balls_thread extends Thread {
protected int mCanvasWidth;
protected int mCanvasHeight;
protected int mRadius;
protected Context context;
public ArrayList<calorie_ball> Calorie_balls = new ArrayList<calorie_ball>(); // Dynamic array with dots
private SurfaceHolder holder;
private boolean running = false;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint text_paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final int refresh_rate = 100; // How often we update the screen, in ms
public base_balls_thread(SurfaceHolder holder, Context ctext, int radius) {
this.holder = holder;
context = ctext;
mRadius = radius;
}
@Override
public void run() {
long previousTime, currentTime;
previousTime = System.currentTimeMillis();
Canvas canvas = null;
while (running) {
// Look if time has past
currentTime = System.currentTimeMillis();
while ((currentTime - previousTime) < refresh_rate) {
currentTime = System.currentTimeMillis();
}
previousTime = currentTime;
try {
// PAINT
try {
canvas = holder.lockCanvas();
synchronized (holder) {
draw(canvas);
}
} finally {
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
// WAIT
try {
Thread.sleep(refresh_rate); // Wait some time till I need to display again
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception eal) {
String msg = eal.getMessage();
if (msg == null)
msg = "Blahbla";
}
}
}
// The actual drawing in the Canvas (not the update to the screen).
private void draw(Canvas canvas) {
// dot temp_dot;
canvas.drawColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(4);
text_paint.setColor(Color.BLACK);
text_paint.setTextSize(40);
try {
for (calorie_ball crcl : Calorie_balls) {
paint.setColor(crcl.color);
paint.setShader(new RadialGradient(crcl.x + 10, crcl.y, crcl.radius * 2, crcl.color, Color.BLACK, Shader.TileMode.CLAMP));
if (crcl.x + crcl.radius < 0 && crcl.y + crcl.radius < 0) {
crcl.x = canvas.getWidth() / 2;
crcl.y = canvas.getHeight() / 2;
} else {
crcl.x += crcl.xVelocity;
crcl.y += crcl.yVelocity;
if ((crcl.x > canvas.getWidth() - crcl.radius) || (crcl.x - crcl.radius < 0)) {
crcl.xVelocity = crcl.xVelocity * -1;
}
if ((crcl.y > canvas.getHeight() - crcl.radius) || (crcl.y - crcl.radius < 0)) {
crcl.yVelocity = crcl.yVelocity * -1;
}
}
String calval = crcl.get_calorie_value();
int x = crcl.x + 5;
int y = crcl.y + 5;
canvas.drawCircle(crcl.x, crcl.y, crcl.radius, paint);
canvas.drawText(calval, x, y, text_paint);
}
} catch (Exception ep) {
String b = ep.getMessage();
if (b == null)
b = "blah";
}
}
public void setRunning(boolean b) {
running = b;
}
protected Canvas myCanvas;
protected Bitmap cvsBmp;
protected Matrix identityMatrix;
public void setSurfaceSize(int width, int height) {
synchronized (holder) {
mCanvasWidth = width;
mCanvasHeight = height;
}
}
}
发生的事情是,如果它只是一个线程…它工作得很好。一旦我介绍了第二条线的组合…比如说一百个球线和五十个球线,这就是一切都变得疯狂的时候
如果你想这么说的话,线程是可以工作的……但是屏幕总是闪烁不定
我知道你们中的一些人可能很清楚这个原因,但不幸的是,我不明白为什么
我假设,因为每个线程都在锁定画布,所以它会等待
有没有办法避开这闪烁的光线?我的设计决定完全错了吗?我确信这是因为每个线程都在访问同一个画布…但我认为这会导致它像那样闪烁。SurfaceView的曲面是双缓冲或三缓冲的。每次调用unlockCanvasAndPost都会向合成器提交一个新的缓冲区。如果每次只渲染场景的1/5,那么我们调用A-B-C-D-E,然后得到一个只有“A”球的帧,然后是一个只有“B”球的帧,依此类推。这假设线程的调度是相当循环的,而在Android/Linux上通常不是这样。我怀疑你看到闪烁是因为你基本上是以50fps的速度运行,一次只显示一组对象 如果每次都不清除画布,那么问题就不那么明显了,因为Android不会为您擦除画布。因此,从前面缓冲区的内容开始,它可能是一组不同的球 画布锁定时,系统提供独占访问。您可以尝试将您不必要的曲面文件夹锁定移动到画布锁定/解锁之外,以查看这是否会有所不同 有关完整的解释,请参阅Android文档 就您的情况而言,虽然可以有多个线程更新状态,如球的位置,但很难让多个线程共享一个画布进行渲染。如果您真的想在软件中完成这一切,请尝试以下方法:创建一个位图,并使用Bresenham或位图自己渲染圆,使用任意多个线程。定期让一个线程冻结位图,锁定画布,并将位图blit到其中
如果您想了解一些简单的2D GLES渲染示例,请参阅或后者,后者使用Bresenham生成圆球纹理。一个问题肯定是canvas.SETCOLOR调用…导致闪烁…但仍然无法按我希望的方式工作。现在我无法清除之前的球位置。感谢您的深入解释。我要查看你的链接。如果这最终导致我找到解决方案,我会在跟进您的建议后接受。感谢您花时间发布。嘿@fadden那么,如果只有一个曲面画布指向此曲面缓冲区来为SurfaceView绘制形状,并且只有一个线程可以通过执行lockCanvas和unlockCanvas访问曲面来绘制,为什么我们需要多个线程?这里不仅仅是普通的景观吗?我遗漏了一些东西?我不确定SurfaceView的曲面和View的曲面视图是否相同thing@solti视图只能从主UI线程更新,而SurfaceView的表面没有该限制。因此,您可以在不暂停用户输入或其他框架操作的情况下,将渲染器线程与昂贵的操作捆绑在一起。您也不太容易受到UI线程活动暂停动画的影响。是的,自定义视图可能比此处的SurfaceView更好。有关为什么让多个线程同时访问同一内存很棘手的信息,请参阅@fadden Yeah,了解在SurfaceView的情况下,我们如何绑定渲染器线程来执行昂贵的绘图操作,而UI线程处理触摸事件、在线测量和在线布局。如果是这样的话,为什么您再次表示自定义视图可能比此处的SurfaceView更好。再一次自定义视图显然不是一个好主意。我在想冲浪 aceView使用2-3线程而不是5线程不确定可能需要收集数据并进行一些研究,但我在这里不是在争论线程的数量