setPivotX works strange on scaled View(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 在缩放视图上工作奇怪
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01