gpt4 book ai didi

java - 使用 ScaleGestureDetector 时放大太多后无法缩小

转载 作者:行者123 更新时间:2023-12-05 00:01:26 26 4
gpt4 key购买 nike

我使用了带有 ScaleGestureDector 类的自定义 View 。我的问题是当我以 4.5 倍放大时,我的自定义布局无法缩小。

这是我的代码:

private class ScaleGestureListener
extends ScaleGestureDetector.SimpleOnScaleGestureListener {

@Override
public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = mScaleFactor * detector.getScaleFactor();

mScaleFactor = scaleFactor;
setScaleX(getScaleX() * mScaleFactor);
setScaleY(getScaleY() * mScaleFactor);

invalidate();
return true;
}

这个问题有什么解决方案吗?我想将我的布局至少放大 4.5 倍

最佳答案

代替:

float scaleFactor = mScaleFactor * detector.getScaleFactor();

这样做:

float scaleFactor = Math.max(4.5F, mScaleFactor * detector.getScaleFactor());

这确保您的最小缩放比例因子为 4.5

编辑:我误解了你的问题。看起来您不应该跟踪 mScaleFactor。 API 为您完成繁重的工作。根据 API,getScaleFactor()返回 从上一个缩放事件到当前事件的缩放因子。

因此,当您在字段中跟踪 mScaleFactor 时,会出现以下问题:

// With these two lines, you're multiplying the variable mScaleFactor.
// Suppose that it was 3, and we're zooming out by 0.66666666
// Now, mScaleFactor is 2.
float scaleFactor = mScaleFactor * detector.getScaleFactor();
mScaleFactor = scaleFactor;

// Here, you're multiplying scaleX and scaleY by mScaleFactor (by 2. This effectively zooms in)
// You should only be multiplying them by 0.666666666 (effectively zooming out).
setScaleX(getScaleX() * mScaleFactor);
setScaleY(getScaleY() * mScaleFactor);

invalidate();
return true;

所以你的 body 应该是这样的:

public boolean onScale(ScaleGestureDetector detector) {
float scaleFactor = detector.getScaleFactor();

setScaleX(getScaleX() * scaleFactor);
setScaleY(getScaleY() * scaleFactor);

invalidate();
return true;
}

关于java - 使用 ScaleGestureDetector 时放大太多后无法缩小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27541461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com