setPivotX works strange on scaled View
@aleien, I can answer your question. When setting a new pivot, view's properties will be recalculated. Android rescale the original view with new pivot but the same scale value which leads to new scaled view has an offset between the old scaled view. The offset is just (getPivotX() - newX) * (1 - getScaleX())
, in order to counteract this offset, we can use setTranslationX()
. Hope my answer can help you!
I solved the problem. Here is the working code:
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
float newX = detector.getFocusX();
float newY = detector.getFocusY();
setTranslationX(getTranslationX() + (getPivotX() - newX) * (1 - getScaleX()));
setTranslationY(getTranslationY() + (getPivotY() - newY) * (1 - getScaleY()));
setPivotX(newX);
setPivotY(newY);
return true;
}
The main problem is to understand how pivot works on scaled views, then there shouldn't be any problems with strange pivot behaviour.