performance - Android Live Wallpaper with canvas.drawBitmap is way to slow -
i started android programming week , curently working on live wallpaper.
i have 2 images, background image (720x1280 png) , slidebar (2276x290 png) slides right left in endles loop. problem slide 3-4fps 1 px per frame (on actual phone, on avd 1/3fps ), wich way choppy live wallpaper. code working fine far way slow.
it nice if know way "a lot" faster.
my code:
private class wengine extends engine { private final handler handler = new handler(); private final runnable drawrunner = new runnable() { @override public void run() { draw(); } }; private paint paint = new paint(); private int width; int height; private boolean visible = true; int x = -1; int x2 = 0; int y = -1; public wengine() { sharedpreferences prefs = preferencemanager .getdefaultsharedpreferences(wservice.this); handler.post(drawrunner); } @override public void onvisibilitychanged(boolean visible) { this.visible = visible; if (visible) { handler.post(drawrunner); } else { handler.removecallbacks(drawrunner); } } @override public void onsurfacedestroyed(surfaceholder holder) { super.onsurfacedestroyed(holder); this.visible = false; handler.removecallbacks(drawrunner); } @override public void onsurfacechanged(surfaceholder holder, int format, int width, int height) { this.width = width; this.height = height; super.onsurfacechanged(holder, format, width, height); } private void draw() { surfaceholder holder = getsurfaceholder(); canvas canvas = null; resources res = getresources(); bitmap backgroundimg = bitmapfactory.decoderesource(res, r.drawable.background); bitmap slidebarimg = bitmapfactory.decoderesource(res, r.drawable.slidebar); try { canvas = holder.lockcanvas(); canvas.drawbitmap(backgroundimg, 0, 0, paint); if (canvas != null) { if (x<0 && y<0) { x = 0; y = math.round(height / 2); } else { x -= 1; } if (x - width < -height * 1.778125) { x2 -= 1; } if (x < -height * 1.778125) { x = x2; x2 = 0; } canvas.drawbitmap(slidebarimg, x, y, paint); if (x2 < 0) { canvas.drawbitmap(slidebarimg, x2, y, paint); } } } { if (canvas != null) holder.unlockcanvasandpost(canvas); } handler.removecallbacks(drawrunner); handler.postdelayed(drawrunner, 30); } }
edit:
after changing images jpg files 6 fps, thats still way slow.
edit 2:
i figured out hardwareaccceleration not possible while using .lockcanvas() there way draw image on livewallpaper (surfaceview) provides hardwareaccceleration?
private void draw() { ... bitmap backgroundimg = bitmapfactory.decoderesource(res, r.drawable.background); bitmap slidebarimg = bitmapfactory.decoderesource(res, r.drawable.slidebar); ...
don't this. if you're decoding bitmaps in every frame, it's going slow. slow. doesn't matter if you're hardware accelerated or not. decode them once, , draw them on each frame.
Comments
Post a Comment