Invalidate-like method for DrawServices method

I tried to make the moving raster image with the accelerometer smoother and accidentally noticed that when I call invalidate(); at the end of the onDraw() method, instead of calling it at the end of onSensorChanged() , I get a much smoother movement, even if I don’t have low-pass filters. Then I tried to do the same with my LiveWallpaper, but, as you know, in the Engine from WallpaperService there is no onDraw() method, but you have to create it yourself and call it, for example, using Handler . But doing it this way does not give any smoother result, even if the rest of the code is the same as in other programs.

This is the code I use in my programs without wallpaper, and it works great:

 public void onDraw(Canvas c) { xPosition += xAcceleration; yPosition += yAcceleration; drawable = BitmapFactory.decodeResource(getResources(),R.drawable.ball); c.drawBitmap(drawable, xPosition,yPosition, paint); invalidate(); } 

So, I went and tried to create my own invalid solution for WallpaperService and came up with the following:

 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(); } 

So what I do:

  • Get Canvas .
  • Draw a Canvas .
  • Unlock Canvas and start over.

As I understand it, this should give me invalidate(); behavior invalidate(); -like, but instead, it tries to show the wallpaper even after it gives me a StackOverflowError.

+4
source share
1 answer

Ok, I already decided that. All I needed to do was translate the initialization of the bitmaps into the onCreate() method.

+1
source

Source: https://habr.com/ru/post/1432698/


All Articles