Drop marker slowly from top of screen to location on android map V2

后端 未结 4 1002
攒了一身酷
攒了一身酷 2021-02-03 16:18

I am using android maps v2works fine I can add and remove markers onLong Touch on locations.

Problem: I would like to drop the marker slowly into the to

4条回答
  •  轮回少年
    2021-02-03 16:53

    I have combined the MaciejGórski's approach with the code from this gist. In addition, added a bounce effect.

    public class MyBounceInterpolator implements android.view.animation.Interpolator {
        double mAmplitude = 1;
        double mFrequency = 10;
    
        public MyBounceInterpolator(double amplitude, double frequency) {
            mAmplitude = amplitude;
            mFrequency = frequency;
        }
    
        public float getInterpolation(float time) {
            double amplitude = mAmplitude;
            if (amplitude == 0) { amplitude = 0.05; }
    
            // The interpolation curve equation:
            //    -e^(-time / amplitude) * cos(frequency * time) + 1
            //
            // View the graph live: https://www.desmos.com/calculator/6gbvrm5i0s
            return (float) (-1 * Math.pow(Math.E, -time/ mAmplitude) * Math.cos(mFrequency * time) + 1);
        }
    }
    
    void dropMarker(final Marker marker, GoogleMap map) {
        final LatLng finalPosition = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
    
        Projection projection = map.getProjection();
        Point startPoint = projection.toScreenLocation(finalPosition);
        startPoint.y = 0;
        final LatLng startLatLng = projection.fromScreenLocation(startPoint);
        final Interpolator interpolator = new MyBounceInterpolator(0.11, 4.6);
    
        TypeEvaluator typeEvaluator = new TypeEvaluator() {
            @Override
            public LatLng evaluate(float fraction, LatLng startValue, LatLng endValue) {
                float t = interpolator.getInterpolation(fraction);
                double lng = t * finalPosition.longitude + (1 - t) * startLatLng.longitude;
                double lat = t * finalPosition.latitude + (1 - t) * startLatLng.latitude;
                return new LatLng(lat, lng);
            }
        };
    
        Property property = Property.of(Marker.class, LatLng.class, "position");
        ObjectAnimator animator = ObjectAnimator.ofObject(marker, property, typeEvaluator, finalPosition);
        animator.setDuration(400);
        animator.start();
    }
    

提交回复
热议问题