Reputation: 3683
Im trying to draw something using SurfaceView and it seems is more and more slow paint like this instead of using onDraw and invalidate of View Class.
So I need to know if i could make it faster. Here's my code.
public class ViewCentroMesa extends SurfaceView implements Runnable
{
// Bucle pintar
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;
public ViewCentroMesa(MesaActivity mesaActivity, Mesa mesa)
{
super(mesaActivity.getContext());
setZOrderOnTop(true);
setWillNotDraw(false);
holder = getHolder();
holder.setFormat(PixelFormat.TRANSLUCENT);
isItOk = true;
t = new Thread(this);
t.start();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
/* Tapete */
try
{
dibujarTapete(canvas);
} catch (Exception e) {}
this.sentido = mesaActivity.getSentidoBitmap();
this.miIcono = mesaActivity.getMiIcono();
this.mazo = mesaActivity.getMazo();
/* Cartas A Robar */
try
{
dibujarCartasARobar(canvas);
} catch (Exception e) {dibujarCartasARobar(canvas);}
/* Sentido */
try
{
dibujarSentido(canvas);
} catch (Exception e) {}
/* Mi icono */
try
{
dibujarMiIcono(canvas);
} catch (Exception e) {}
//if (getMesa().isJuegoAcabado()) return;
boolean cond = mesa.isMiJugadorAcabo();
if (cond) return;
/*Cartas Tiradas */
try
{
dibujarCartasTiradas(canvas);
} catch (Exception e) {e.printStackTrace();}
/*Carta mesa */
try
{
dibujarCartaMesa(canvas);
} catch (Exception e) {}
/* Carta Tirada*/
try
{
dibujarCartaTirada(canvas);
} catch (Exception e) {}
/* Mazo */
try
{
dibujarMazo(canvas);
} catch (Exception e) {}
/* Repartiendo o robando*/
try
{
dibujarAnimacionRobarRepartir(canvas);
} catch (Exception e) {}
}
@SuppressLint("WrongCall")
@Override
public void run()
{
while (true)
{
// Dibujar
if (!holder.getSurface().isValid())continue;
if (!isItOk) continue;
Canvas c = holder.lockCanvas();
synchronized (holder) {
onDraw(c);
}
holder.unlockCanvasAndPost(c);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void pause ()
{
//isItOk = false;
}
public void resume ()
{
//isItOk = true;
}
}
Upvotes: 0
Views: 64
Reputation: 52353
Don't override onDraw()
if you want to draw on the SurfaceView's Surface.
SurfaceViews have two parts, the Surface and the View. The View part works like it does in any other member of the View class, obeying the usual invalidate / redraw sequence. Normally, the SurfaceView's View is a transparent hole that is used by the layout code to leave gap for the Surface to show through.
The Surface is a completely separate layer, which by default sits behind the View layer, but can be configured to be on top. It ignores invalidate / refresh, updating whenever you unlock it (or swap EGL buffers if you're using GLES). Because there's no interaction with the View UI, you can update the Surface from a dedicated renderer thread.
You're mixing the two together. You've overridden onDraw()
, as if you were creating a custom View, but you're also rendering onto the Surafce with the same function.
Rename onDraw()
to doDraw()
and drop the override. Alternatively, switch from SurfaceView to a plain custom View, and drive your updates with postInvalidate()
.
Canvas rendering onto a SurfaceView Surface is not hardware-accelerated, but Canvas drawing on a custom View is. Depending on your device and how many pixels you're touching it may be faster to use a custom View.
You can also speed up Surface rendering by using a smaller Surface and letting the hardware scale it. (demo, blog post)
Upvotes: 2