Android pinch zoom on Camera delay -
i have custom surfaceview camerapreview in app, , trying implement pinch zoom, implementing these 2 methods:
@override public boolean ontouchevent(motionevent event) { camera camera = getcamera(); if (camera == null) { return true; } camera.parameters params = camera.getparameters(); int action = event.getaction(); if (event.getpointercount() > 1) { if (action == motionevent.action_pointer_down) { mclog.v(tag, "single "); mdist = getfingerspacing(event); mclog.w(tag, "original distance " + mdist); } else if (action == motionevent.action_move && params.iszoomsupported()) { camera.cancelautofocus(); handlezoom(event, params); } } else { if (action == motionevent.action_up) { mfirsttime = false; handlefocus(event, params); } } return true; } private void handlezoom(motionevent event, camera.parameters params) { if(mfirsttime) { mdist = getfingerspacing(event); mfirsttime = false; return; } list<integer> zoomratios = params.getzoomratios(); int maxzoom = params.getmaxzoom(); int zoom = params.getzoom(); double spacing = getfingerspacing(event); mclog.w(tag, string.format("old zoom is: %s", zoom)); //percentage of displacement mclog.w(tag, string.format("original distance is: %s, new displacement %s", mdist, spacing)); double percentage = (mdist + spacing)/mdist; if(mdist > spacing) { percentage *= -1; } mclog.w(tag, string.format("percentage is: %s", percentage)); zoom = new double(zoom + percentage).intvalue(); mclog.w(tag, string.format("new zoom is: %s", zoom)); if (zoom > maxzoom) { zoom = maxzoom; } if (zoom < 0) { zoom = 0; } mdist = spacing; params.setzoom(zoom); if (mzoomlistener != null) { mzoomlistener.onzoomchanged(zoomratios.get(zoom)); } getcamera().setparameters(params); }
this seems working, zoom has slight delay gets longer more zoom image. stop pinching , image still keep zooming in.
i couldnt find implementation pinch zoom in camera besides this one, maybe doing wrong.
since you're seeing logging continue after lift finger, means you're not processing touch event queue fast enough.
that setproperties call not particularly fast.
so you'll need rate-limit somehow, , drop touch events don't have time handle. there many options of varying kinds of tradeoffs.
i'm not familiar input apis, not sure if there's parameter can tweak reduce rate of calls - maybe don't unless change in zoom above dinner threshold, , increase threshold until zooming doesn't lag?
or can send zoom calls thread invoke setparameters, , drop zoom call floor if thread if busy processing previous call.
or better, have 'nextzoom' parameter zoom setting thread looks @ once finishes prior call, , have touch event handler update nextzoom on each invocation. zoom setting thread checks if value has changed once finishes last set call, , if so, sets again.
then you'll newest zoom level, , won't pile either.
Comments
Post a Comment