setPivotX 在缩放视图上工作奇怪

setPivotX works strange on scaled View(setPivotX 在缩放视图上工作奇怪)

本文介绍了setPivotX 在缩放视图上工作奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 setPivotX(也是 setPivotY)在 Android 中的工作方式很奇怪.如果在视图的比例设置为 1.00f 时设置枢轴,则不会发生任何事情(只是枢轴更改).但是,如果比例不等于 1.0f(例如 setScaleX(0.9f))并且您设置了枢轴,则视图相对(?)移动到新的枢轴.是不是很奇怪?我知道水平和垂直位置(平移)与枢轴值无关,但为什么视图以 1.0f 以外的比例因子移动?

I found out that setPivotX (also setPivotY) works strange in Android. If you set pivot when view's scale is set to 1.00f nothing happens (just pivot changes). But if the scale isn't equal 1.0f (e.g. setScaleX(0.9f)) and you set the pivot the view moves relatively(?) to the new pivot. Isn't it strange? I know that horizontal and vertical positions (translations) are NOT related to the pivot value, but why the view moves with scale factor other than 1.0f?

请检查是否有缩放部分.

Please check this out with and without the scaling part.

public class ScaleView extends View {

private final ScaleGestureDetector mScaleGestureDetector;

public ScaleView(Context context, AttributeSet attrs) {
    super(context, attrs);


    //setScaleX(0.9f);
    //setScaleY(0.9f);

    mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.OnScaleGestureListener() {

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            // does nothing intentionally
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            setPivotX(detector.getFocusX());
            setPivotY(detector.getFocusY());
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            return false;
        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mScaleGestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}
}

如何设置视图的相同位置,即枢轴更改之前的位置?

How do I set the same position of the view, which was before the pivot changed?

推荐答案

我解决了这个问题.这是工作代码:

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.

这篇关于setPivotX 在缩放视图上工作奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:setPivotX 在缩放视图上工作奇怪

基础教程推荐