setPivotX工程缩放视图怪 [英] setPivotX works strange on scaled View

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

问题描述

我发现 setPivotX (也 setPivotY )的Andr​​oid的工作奇怪。如果设置支点,当视图的比例设置为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?

推荐答案

我解决了这个问题。这里是工作code:

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工程缩放视图怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆