I am working on a livewallpaper for Android. The code works, but I have performance issues. Basically I am drawing Bitmaps and move them. With 15 small images it workes fine. But with 50 bigger images it starts lagging.
In my moving object I am creating the Bitmap in the constuctor and display it like this :
public void drawFrame(Canvas canvas) {
Position p = movingStrategy.move();
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
matrix.postRotate(p.getRotation());
matrix.postTranslate(p.getPositionX(), p.getPositionY());
canvas.drawBitmap(bitmap, matrix, paint);
}
And in my wallpaperservice I am calling the onDraw like this:
private void draw() {
handler.removeCallbacks(drawRunner);
SurfaceHolder holder = getSurfaceHolder();
Canvas canvas = null;
try {
canvas = holder.lockCanvas();
if (canvas != null) {
canvas.save();
canvas.drawColor(Color.BLACK);
for (DrawElement element : elements) {
element.drawFrame(canvas);
}
canvas.restore();
}
} finally {
if (canvas != null)
holder.unlockCanvasAndPost(canvas);
}
if (visible) {
handler.postDelayed(drawRunner, 1);
}
}
I tried to use this with a normal View and it's onDraw and there it works fine. It's very smooth. Now I am asking myself how the performance can be improved. I also tried different delayMillis, but the performance doesn't increase.
I have also heard about GLWallpaperService, but this seems very complicated to draw simple Bitmaps. So I want to use Canvas instead.
Thank you
Edit:
I've tested the performance between the view and the service. This part:
long start = System.currentTimeMillis();
for (DrawElement element : elements) {
element.drawFrame(canvas);
}
Log.e("DrawingTime", Long.toString(System.currentTimeMillis()-start));
takes in the view 0-1ms and in the service between 50 and 300ms.