3

我在自定义视图中有以下代码,它是一个 ScaleGestureDetector ,我想要做的是在你捏屏幕时更改视图的宽度和高度,但 ScaleFactor 总是趋向于一个,我不知道如何解释它,但是 ScaleFactor 在几秒钟内低于/高于 1,然后再次变为 1。您可以在 logCat 输出中看到我在说什么。

代码:

public class simpleOnScaleGestureListener extends SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float ScaleFactor = 1.0f;
        ScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
          ScaleFactor = Math.max(0.1f, Math.min(ScaleFactor, 5.0f));


        Log.d("ScaleFactor", "The ScaleFactor is "+ScaleFactor);

    return true;
    }
    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {

    }
}

LogCat 输出:

05-29 19:37:48.019: D/ScaleFactor(7279): The ScaleFactor is 1.0247091
05-29 19:37:48.059: D/ScaleFactor(7279): The ScaleFactor is 1.0090069
05-29 19:37:48.069: D/ScaleFactor(7279): The ScaleFactor is 1.0268435
05-29 19:37:48.109: D/ScaleFactor(7279): The ScaleFactor is 1.0389019
05-29 19:37:48.119: D/ScaleFactor(7279): The ScaleFactor is 1.0236202
05-29 19:37:48.159: D/ScaleFactor(7279): The ScaleFactor is 1.0267988
05-29 19:37:48.169: D/ScaleFactor(7279): The ScaleFactor is 1.0133468
05-29 19:37:48.199: D/ScaleFactor(7279): The ScaleFactor is 1.0230283
05-29 19:37:48.219: D/ScaleFactor(7279): The ScaleFactor is 1.003792
05-29 19:37:48.599: D/ScaleFactor(7279): The ScaleFactor is 1.0
05-29 19:37:48.629: D/ScaleFactor(7279): The ScaleFactor is 1.2110677
05-29 19:37:48.639: D/ScaleFactor(7279): The ScaleFactor is 1.2486423
05-29 19:37:48.679: D/ScaleFactor(7279): The ScaleFactor is 1.0709958
05-29 19:37:48.689: D/ScaleFactor(7279): The ScaleFactor is 1.094618
05-29 19:37:49.229: D/ScaleFactor(7279): The ScaleFactor is 1.0
05-29 19:37:49.259: D/ScaleFactor(7279): The ScaleFactor is 1.2032005
05-29 19:37:49.269: D/ScaleFactor(7279): The ScaleFactor is 1.1687788
05-29 19:37:49.309: D/ScaleFactor(7279): The ScaleFactor is 1.0600644
05-29 19:37:49.319: D/ScaleFactor(7279): The ScaleFactor is 1.0608224
05-29 19:37:50.049: D/ScaleFactor(7279): The ScaleFactor is 1.0
05-29 19:37:50.079: D/ScaleFactor(7279): The ScaleFactor is 1.3738323
05-29 19:37:50.089: D/ScaleFactor(7279): The ScaleFactor is 1.1507335
05-29 19:37:50.129: D/ScaleFactor(7279): The ScaleFactor is 1.2026775
05-29 19:37:50.899: D/ScaleFactor(7279): The ScaleFactor is 1.0
05-29 19:37:50.929: D/ScaleFactor(7279): The ScaleFactor is 1.5862415
05-29 19:37:50.939: D/ScaleFactor(7279): The ScaleFactor is 1.1621073
05-29 19:37:52.559: D/ScaleFactor(7279): The ScaleFactor is 1.0
05-29 19:37:52.599: D/ScaleFactor(7279): The ScaleFactor is 1.0097717
05-29 19:37:52.599: D/ScaleFactor(7279): The ScaleFactor is 1.0186267
4

2 回答 2

3

float ScaleFactor = 1.0f;

您在每次捏合开始时重置比例。尝试全局变量。

于 2016-05-16T13:13:31.227 回答
2

此代码来自 Android Tutorial on Dragging and Scaling,但有一个关键区别:“ScaleFactor”局部变量(旁注 - 约定是以小写字母开头的变量名)被声明为实例/成员变量mScaleFactor用于原始代码中的整个类('m' 代表 Android 中的成员)。

预期的结果是该行:

ScaleFactor *= detector.getScaleFactor();

将在手势过程中将各个事件比例因子累积为一个有意义的值,而在您的版本中,它会在每次调用 onScale() 时重置,因此您只能看到每个单独的缩放事件创建的瞬时值。

如果这仍然有点令人费解,那么请查看基本的 Java 教科书,了解不同类型的变量及其范围的描述。

于 2013-07-23T19:29:38.397 回答