简体   繁体   English

PointF.set(x,y)与Java中新的PointF(x,y)有何不同?

[英]How is PointF.set(x,y) different from new PointF(x,y) in java?

I am trying to implement a scroll for my Canvas View in an Android app. 我正在尝试在Android应用程序中为我的Canvas视图实现滚动。

I initialize a global 我初始化一个全局

private PointF backgroundPosition = new PointF(0, 0);

and then the behavior of this 然后这个的行为

backgroundPosition.set(lastBackgroundPosition.x + (canvasMovingStartingPoint.x - event.getX()), lastBackgroundPosition.y + (canvasMovingStartingPoint.y - event.getY()));

and this 和这个

backgroundPosition = new PointF(lastBackgroundPosition.x + (canvasMovingStartingPoint.x - event.getX()), lastBackgroundPosition.y + (canvasMovingStartingPoint.y - event.getY()));

is different for some reason (the scroll of the first version is much faster!!!) 由于某些原因而有所不同(第一个版本的滚动速度更快!!!)

WHY?!!! 为什么?!!! I have spent the whole day for it! 我已经花了一整天了!

Here is the complete code: 这是完整的代码:

    public boolean onTouchEvent(@NonNull MotionEvent event, GII.AppState appState) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            pressedHere = true;
            canvasMovingStartingPoint.set((int) event.getX(), (int) event.getY());
            lastBackgroundPosition = backgroundPosition;
            break;
        case MotionEvent.ACTION_MOVE:
            if (pressedHere &&
                    Math.sqrt((canvasMovingStartingPoint.x - event.getX()) * (canvasMovingStartingPoint.x - event.getX()) +
                            (canvasMovingStartingPoint.y - event.getY()) * (canvasMovingStartingPoint.y - event.getY())) > 10)
                moving = true;
            if (pressedHere && moving) {
                backgroundPosition.set(lastBackgroundPosition.x + (canvasMovingStartingPoint.x - event.getX()), lastBackgroundPosition.y + (canvasMovingStartingPoint.y - event.getY()));
                //backgroundPosition = new PointF(lastBackgroundPosition.x + (canvasMovingStartingPoint.x - event.getX()), lastBackgroundPosition.y + (canvasMovingStartingPoint.y - event.getY()));
                checkBackground();
            }
            break;
        case MotionEvent.ACTION_UP:
            if (pressedHere && !moving) {
                click(event.getX(), event.getY());
            }
            moving = false;
            break;
        default:
            return false;
    }
    return true;
}

Uncommenting that line makes everything perfect, the question is why? 取消注释这条线会使一切完美,问题是为什么?

In the bottom example you are creating a new instance of PointF 在底部的示例中,您正在创建PointF的新实例。

In the top one your just change the instances value. 在顶部,您只需更改实例值。

Creating instances can be much more costly in memory then just reusing them so You are redundantly creating excess objects It is a low cost, but you should avoid creating unnecessary objects. 创建实例在内存中的成本可能会比仅重用它们高得多,因此您将多余地创建多余的对象,这是一种低成本,但应避免创建不必要的对象。 Same as you should avoid unnecessary anything in your code. 与应该避免代码中不必要的内容相同。

As another note, from my understanding the x,y values in a PointF are final so they cannot be changed. 另一个要注意的是,根据我的理解,PointF中的x,y值是最终值,因此无法更改。 So it appears the 2nd option is the way you have to go with since the set method appears to only work with empty instances. 因此,似乎第二个选项是您必须使用的方法,因为set方法似乎仅适用于空实例。 If it slows your program down to much you could always store the values in just two floats. 如果它使程序变慢,您总是可以将值只存储在两个浮点中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM