android – 如何通过LatLng点的ArrayList为标记设置动画?

android – 如何通过LatLng点的ArrayList为标记设置动画?,第1张

概述我正在使用Google Maps API V2来获取当前用户的位置,并使用onLocationChanged侦听器记录他的路线.当用户记录他们的路线时,我将在每个位置检测到的所有LatLng保存在一个arraylist中.当用户停止记录他们的路线时,我在arraylist的第一个点放置一个标记.我现在的问题是我希望通过arraylist中的所有点来设置标记的动画.有人可以告诉我,我该怎么做? 需要 我正在使用Google Maps API V2来获取当前用户的位置,并使用onLocationChanged侦听器记录他的路线.当用户记录他们的路线时,我将在每个位置检测到的所有LatLng保存在一个arrayList中.当用户停止记录他们的路线时,我在arrayList的第一个点放置一个标记.我现在的问题是我希望通过arrayList中的所有点来设置标记的动画.有人可以告诉我,我该怎么做?

需要注意的是阵列不需要排序,因为我记录了它们来的点.我已经尝试使用for循环遍历数组并发送值,但是我得到了一个错误

final LatLng startLatLng = proj.fromScreenLocation(startPoint);

说“UnkNown Source”和NullPointer异常.

这是我的代码:

case R.ID.action_replay:            int o;            for(o=0; o<oldlocPoints.size(); ++o){                if(--o > 1){lastPos = oldlocPoints.get(--o);}                topos = oldlocPoints.get(++o);                animateMarker(markerStart,lastPos,topos);            } return true;

这就是我如何尝试通过标记进行动画制作.我遇到的主要困难是在run()中它似乎只是想要最终类型值,所以我不知道如何取悦它.

//Animates marker through the locations saved from the recorded routepublic voID animateMarker(final Marker marker,LatLng lastPos,final LatLng topos) {    final long duration = 1600;    final Handler handler = new Handler();    this.lastPos = lastPos;    this.topos = topos;    final long start = SystemClock.uptimeMillis();    final int o;    Projection proj = map.getProjection();    Point startPoint = proj.toScreenLocation(lastPos);    final LatLng startLatLng = proj.fromScreenLocation(startPoint);    Log.d(TAG,"" + lastPos + "" + topos);    final Interpolator interpolator = new AccelerateDecelerateInterpolator();    handler.post(new Runnable() {        @OverrIDe        public voID run() {            long elapsed = SystemClock.uptimeMillis() - start;            float t = interpolator.getInterpolation((float) elapsed / duration);            double lng = t * topos.longitude + (1 - t) * startLatLng.longitude;            double lat = t * topos.latitude + (1 - t) * startLatLng.latitude;            markerStart.setposition(new LatLng(lat,lng));            //markerStart.setposition(interpolator.interpolate(t,target,replayEnd));            if (t < 1.0) {                //Post again 16ms later == 60 frames per second                handler.postDelayed(this,32);            } else {                //Animation ended            }        }    });}

有人可以帮帮我吗?

UPDATE
我最近的尝试是这样的:

while (i<oldlocPoints.size()){                final long duration = 32;                final Handler handler = new Handler();                final long start = SystemClock.uptimeMillis();                Projection proj = map.getProjection();                final LatLng topos = oldlocPoints.get(i/3);                Point startPoint = proj.toScreenLocation(oldlocPoints.get(i));                final LatLng startLatLng = proj.fromScreenLocation(startPoint);                final Interpolator interpolator = new AccelerateDecelerateInterpolator();                handler.post(new Runnable() {                    @OverrIDe                    public voID run() {                        long elapsed = SystemClock.uptimeMillis() - start;                        float t = interpolator.getInterpolation((float) elapsed / duration);                        double lng = t * topos.longitude + (1 - t) * startLatLng.longitude;                        double lat = t * topos.latitude + (1 - t) * startLatLng.latitude;                        markerStart.setposition(new LatLng(lat,lng));                        //markerStart.setposition(interpolator.interpolate(t,replayEnd));                        if (t < 1.0) {                            //Post again 16ms later == 60 frames per second                            handler.postDelayed(this,32);                        } else {                            //Animation ended                        }                    }                });                i++;            }
解决方法 试试这个代码::
首先是MarkerAnimation类:

public class MarkerAnimation {    static GoogleMap map;    ArrayList<LatLng> _trips = new ArrayList<>() ;    Marker _marker;    LatLngInterpolator _latLngInterpolator = new LatLngInterpolator.Spherical();public voID animateline(ArrayList<LatLng> Trips,GoogleMap map,Marker marker,Context current){    _trips.addAll(Trips);    _marker = marker;    animateMarker();}        public voID animateMarker() {            TypeEvaluator<LatLng> typeEvaluator = new TypeEvaluator<LatLng>() {                @OverrIDe                public LatLng evaluate(float fraction,LatLng startValue,LatLng endValue) {                    return _latLngInterpolator.interpolate(fraction,startValue,endValue);                }            };            Property<Marker,LatLng> property = Property.of(Marker.class,LatLng.class,"position");            ObjectAnimator animator = ObjectAnimator.ofObject(_marker,property,typeEvaluator,_trips.get(0));            //ObjectAnimator animator = ObjectAnimator.o(vIEw,"Alpha",0.0f);            animator.addListener(new Animator.AnimatorListener() {                @OverrIDe                public voID onAnimationCancel(Animator animation) {                    //  animDrawable.stop();                }                @OverrIDe                public voID onAnimationRepeat(Animator animation) {                    //  animDrawable.stop();                }                @OverrIDe                public voID onAnimationStart(Animator animation) {                    //  animDrawable.stop();                }                @OverrIDe                public voID onAnimationEnd(Animator animation) {                    //  animDrawable.stop();                    if (_trips.size() > 1) {                        _trips.remove(0);                        animateMarker();                    }                }            });            animator.setDuration(300);            animator.start();        }

以及谷歌开发人员为您预先编写的_latLngInterpolator:

public interface LatLngInterpolator {    public LatLng interpolate(float fraction,LatLng a,LatLng b);    public class Spherical implements LatLngInterpolator {        @OverrIDe        public LatLng interpolate(float fraction,LatLng from,LatLng to) {            // http://en.wikipedia.org/wiki/Slerp            double fromLat = toradians(from.latitude);            double fromLng = toradians(from.longitude);            double tolat = toradians(to.latitude);            double tolng = toradians(to.longitude);            double cosFromLat = cos(fromLat);            double cosTolat = cos(tolat);            // Computes Spherical interpolation coefficIEnts.            double angle = computeAngleBetween(fromLat,fromLng,tolat,tolng);            double sinAngle = sin(angle);            if (sinAngle < 1E-6) {                return from;            }            double a = sin((1 - fraction) * angle) / sinAngle;            double b = sin(fraction * angle) / sinAngle;            // Converts from polar to vector and interpolate.            double x = a * cosFromLat * cos(fromLng) + b * cosTolat * cos(tolng);            double y = a * cosFromLat * sin(fromLng) + b * cosTolat * sin(tolng);            double z = a * sin(fromLat) + b * sin(tolat);            // Converts interpolated vector back to polar.            double lat = atan2(z,sqrt(x * x + y * y));            double lng = atan2(y,x);            return new LatLng(todegrees(lat),todegrees(lng));        }        private double computeAngleBetween(double fromLat,double fromLng,double tolat,double tolng) {            // haversine's formula            double dLat = fromLat - tolat;            double dLng = fromLng - tolng;            return 2 * asin(sqrt(pow(sin(dLat / 2),2) +                    cos(fromLat) * cos(tolat) * pow(sin(dLng / 2),2)));        }    }}

并在您的地图活动中将其命名为:

MarkerAnimation.animateline(TripPoints,map,MovingMarker,context);
总结

以上是内存溢出为你收集整理的android – 如何通过LatLng点的ArrayList为标记设置动画?全部内容,希望文章能够帮你解决android – 如何通过LatLng点的ArrayList为标记设置动画?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1130026.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存