Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalidate-like method for WallpaperServices' draw method
    primarykey
    data
    text
    <p>I was trying to make moving bitmap with accelerometer smoother and accidentally noticed that when I call <code>invalidate();</code> at the end of <code>onDraw()</code> method instead of calling it at the end of <code>onSensorChanged()</code> I get much smoother movement, even if I don't have any kind of low-pass filters. Then I tried to do the same with my LiveWallpaper, but as you know there is no <code>onDraw()</code> method in <code>Engine</code> of <code>WallpaperService</code>, but you have to create one yourself and call it for example with <code>Handler</code>. But doing it that way doesn't give any smoother result even if the rest of the code is same as in other programs.</p> <p>This is the code that I use in my non-Wallpaper programs and it works fine:</p> <pre><code>public void onDraw(Canvas c) { xPosition += xAcceleration; yPosition += yAcceleration; drawable = BitmapFactory.decodeResource(getResources(),R.drawable.ball); c.drawBitmap(drawable, xPosition,yPosition, paint); invalidate(); } </code></pre> <p>So I went and tried to create my own invalidate-like solution for <code>WallpaperService</code> and came up with this:</p> <pre><code>void drawFrame() { final SurfaceHolder holder = getSurfaceHolder(); Canvas c = null; try { c = holder.lockCanvas(); if (c != null) { xPosition += xAcceleration; yPosition += yAcceleration; background = BitmapFactory.decodeResource(getResources(),R.drawable.bg); drawable = BitmapFactory.decodeResource(getResources(),R.drawable.ball); c.drawBitmap(background, 0,0, null); c.drawBitmap(drawable, xPosition,yPosition, null); } } catch (Exception ex){ } holder.unlockCanvasAndPost(c); drawFrame(); } </code></pre> <p>So what I am doing is:</p> <ol> <li>Get <code>Canvas</code>.</li> <li>Draw on <code>Canvas</code>.</li> <li>Unlock <code>Canvas</code> and start over.</li> </ol> <p>As I have understood this should give me <code>invalidate();</code>-like behaviour, but instead it tries to show wallpaper and after while it gives me StackOverflowError.</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