Note that there are some explanatory texts on larger screens.

plurals
  1. POSurfaceView, onDraw and postInvalidate() something wrong
    primarykey
    data
    text
    <p>App behave itself strangely. I have a class extends the SurfaceView and implements SurfaceHolder.Callback .<br> I tried 3 options of code and only one works like i need (but I'm doubt that it is a right solution) and other have different behavior.<br> So, my question is why and what is wrong and if "right" solution is wrong, so why it's work...<br><p> Right for all:</p> <ol> <li>I tried <code>setWillNotDraw(boolean flag)</code> flag(true/false) </li> <li>In manifest set <code>android:changeConfiguration="orientation"</code></li> <li>In main layout set background with tiling picture defined via xml resources.</li> </ol> <p>Now the "right" implementation: I have thread which call <code>postInvalidate();</code> with delays and calls between lockCanvas and unlockCanvas:<br> my onDraw() and draw(Canvas c,int deg) same in all variations:</p> <pre><code>@Override public void onDraw(Canvas c) { draw(c,lastHeading); super.onDraw(c); } public synchronized void draw(Canvas c,int degrees) { Paint p=new Paint(); p.setAntiAlias(true); c.save(); rotateLayer(degrees,layer1, cLayers.getDrawable(0),false); rotateLayer(lastSideAngle, layer2, sLayers.getDrawable(1),true); rotateLayer(lastFrontAngle, layer2, fLayers.getDrawable(1),true); c.drawBitmap(layer1,0, 0, p); c.drawBitmap(layer2,0, 0, p); c.drawBitmap(layer3, 0,0, p); c.restore(); } </code></pre> <p>Problem code variant A (and "right" ):</p> <pre><code>@Override public void run() { Canvas c=null; try { c=holder.lockCanvas(null); synchronized (holder) { postInvalidate(); } finally { if(c!=null) holder.unlockCanvasAndPost(c); } } </code></pre> <p>why it works without problems however same code without lock/unloc of canvas didn't work corectly? <p> With next implementation app work nice until phone doesn't change orientation. While changing orientation its take along time to redraw the surface, but in next orientation change it changes fast. but in both variants i call to postInvalidate() and I not understand how lock/unlock Canvas affect on postInvalidate() ?</p> <pre><code>@Override public void run() { postInvalidate(); } </code></pre> <p><p> And in this variant I see on screen a frozen image which never cahnges:</p> <pre><code>@Override public void run() { Canvas c=null; try { c=holder.lockCanvas(null); synchronized (holder) { draw(c,deg); //OR //onDraw(c) } finally { if(c!=null) holder.unlockCanvasAndPost(c); } } </code></pre> <p>There is one more variant when app draws the all bitmaps but a background is overlayed its all. So what i do wrong and what i need to do?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload