Achieve similar hardware acceleration optimization in Android 2.3 - Avoid multiple onDraw call -
by referring http://developer.android.com/training/custom-views/optimizing-view.html#accelerate, know can avoid multiple system call on busy ondraw
using setlayertype(view.layer_type_hardware, null);
.
i tested on android 4, performing home -> restore -> home. realize ondraw
called once stated in above android documentation.
however, how can achieve similar optimization in android 2.3? android 2.3 doesn't support hardware optimization.
public class piechart extends view { @suppresslint("newapi") public piechart(context context) { super(context); mpiepaint = new paint(); mpiepaint.setantialias(true); mpiepaint.setstyle(paint.style.fill); mpiepaint.setcolor(0x88ff0000); if (android.os.build.version.sdk_int >= android.os.build.version_codes.honeycomb) { if (!isineditmode()) { setlayertype(view.layer_type_hardware, null); } } } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); log.i("cheok", "busy ondraw called"); canvas.drawarc(mbounds, 0, 200, true, mpiepaint); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { mbounds = new rectf(0, 0, w, h); } private rectf mbounds; private paint mpiepaint; }
what should try 2 versions of class.
the first 1 used hardware-accelerated devices, , use optimisation explained in google documentations
for devices not support such optim, use second version of 'pieview', ondraw method draws content on transparent buffer bitmap first, before copying bitmap on canvas. costly drawing occur once, , unchanged bitmap reused on subsequent calls. of course, if change must made cache image (for instance if onsizechanged called), must computed again.
Comments
Post a Comment