android - 从点到点 B 的 TranslateAnimation

android - TranslateAnimation from PointA to pointB

我想将视图从 A 点转换到 B 点。假设从 A 点到 B 点画了一条线。视图应该沿着那条不可见的线转换到 pointB。我怎样才能做到这一点。这是我到目前为止尝试过的方法:

getView().animate().translationX(deltaX)
getView().animate().translationY(deltaY)

但我没有得到我想要的结果。具体来说,我正在使用 google 地图,我想使用翻译将一个标记移动到另一个位置。

我想你的方式一定更复杂才能实现这个目标。 需要在 MapView 上方绘制一个透明的覆盖物,该覆盖物必须是可点击和可聚焦的,以接收触摸事件并防止地图因用户输入(例如,FrameLayout 或自定义 View)而发生变化。然后在这个Surface上画一个"fake marker",动画,最后销毁它们。

接下来是技巧:

  1. 你在 A

  2. 点有一个标记
  3. 在所有地图上方显示表面以防止触摸

  4. 在此表面上绘制新视图 "fake marker"(在原始标记之上)

  5. 从地图中删除原始标记

  6. Animate/Translate 你的新标记指向 B

  7. 在地图上的 B

  8. 点绘制一个新标记
  9. 隐藏表面 "fake marker"

我相信this repo会帮助你实现你想要的目标。

i am using a google map and i want to move one marker to another location

我认为 MapUtils.java 中的 animateMarker() 方法可以帮助您实现这一目标,

public static void animateMarker(final Location destination, final Marker marker) {
        if (marker != null) {
            final LatLng startPosition = marker.getPosition();
            final LatLng endPosition = new LatLng(destination.getLatitude(), destination.getLongitude());

            final float startRotation = marker.getRotation();

            final LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
            ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
            valueAnimator.setDuration(1000); // duration 1 second
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override public void onAnimationUpdate(ValueAnimator animation) {
                    try {
                        float v = animation.getAnimatedFraction();
                        LatLng newPosition = latLngInterpolator.interpolate(v, startPosition, endPosition);
                        marker.setPosition(newPosition);
                        marker.setRotation(computeRotation(v, startRotation, destination.getBearing()));
                    } catch (Exception ex) {
                        // I don't care atm..
                    }
                }
            });

            valueAnimator.start();
        }
}