Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does calling setScaleX during pinch zoom gesture cause flicker?

I am trying to create a zoomable container and I am targeting API 14+

In my onScale (i am using the ScaleGestureDetector to detect pinch-zoom) I am doing something like this:

public boolean onScale (ScaleGestureDetector detector) {
   float scaleFactor = detector.getScaleFactor();
   setScaleX(getScaleX() * scaleFactor);
   setScaleY(getScaleY() * scaleFactor);

   return true;
};

It works but the zoom is not smooth. In fact it noticeably flickers.

I also tried it with hardware layer thinking that the scaling would happen on the GPU once the texture was uploaded and thus would be super fast. But it made no difference - the zoom is not smooth and flickers weirdly sometimes.

What am I doing wrong?

like image 677
numan salati Avatar asked Jun 30 '13 22:06

numan salati


People also ask

Why does my Zoom flicker?

Several people experienced screen flickering when using Zoom conferencing and a zSpace system when they enabled screen sharing. Because of its higher refresh rate, zSpace systems are typically unable to handle a refresh rate that is high enough to handle Zoom's system.

What happens when you pinch to Zoom?

What is pinch-to-zoom? Pinch-to-zoom is when a user uses two fingers and pinches to zoom in or out on their mobile device. While it wasn't present on the original touchscreen phones, pinch-to-zoom is now a nearly universal motion for adjusting size on touchscreen devices.

How do you pinch Zoom on Android?

Tap anywhere on the screen, except the keyboard or navigation bar. Drag 2 fingers to move around the screen. Pinch with 2 fingers to adjust zoom. To stop magnification, use your magnification shortcut again.


1 Answers

Does the flicker look like the view flipping back and forth between zoomed and not-zoomed? That's caused by having the ScaleGestureDetector handling motion events from the same view that you're scaling. When you setScaleX() that changes the coordinates of the touch, which triggers a new touch event interpreted as reversing the zoom you just applied.

Put the content of your zoomable view inside a single child FrameLayout and set the scale on that view instead.

I've posted a working pinch-zoom layout here: https://gist.github.com/anorth/9845602

like image 165
Alex North Avatar answered Sep 30 '22 17:09

Alex North