如何在android java上使用新的折线代码

pw9qyyiw  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(207)

嗨,我刚开始做应用程序,所以我需要所有能得到的帮助。正如标题所说,我需要删除我的旧折线代码,我在我的应用程序中,以取代新的,但我不能使它工作。
我在github项目中找到了一个项目
我用这个代码来做一条折线

@Override
public void onDirectionSuccess(Direction direction, String rawBody) {
    if (direction.isOK()) {
        Route route = direction.getRouteList().get(0);

        try {
            JSONObject obj = new JSONObject(rawBody);

            routeData = parseJson(obj);

            mAdapter.setData(routeData);
            mAdapter.notifyDataSetChanged();

            Log.d("My App", obj.toString());

        } catch (Throwable ignored) {
        }

        ArrayList<LatLng> directionPositionList = route.getLegList().get(0).getDirectionPoint();
        Polyline polyline = mMap.addPolyline(DirectionConverter.createPolyline(this, directionPositionList, 5, Color.BLUE));
        polylines.add(polyline);
        setCameraWithCoordinationBounds(route);
    }
}

如何删除此代码并调用新的多段线。我试图删除它,并粘贴在这里的孔代码,但它没有工作。我也删除了代码,只是尝试启动模拟器,但它没有给出任何折线。所以我现在被困在这里。我还创建了两个新的java类,就像他在github上所做的那样,我想知道如何调用这些类来获得折线?
这是我想使用的新代码
公共类mapanimator{

static final int GREY = Color.parseColor("#FFA7A6A6");
public static MapAnimator mapAnimator;
private Polyline backgroundPolyline;
private Polyline foregroundPolyline;
private PolylineOptions optionsForeground;
private AnimatorSet firstRunAnimSet;
private AnimatorSet secondLoopRunAnimSet;
Context mContext;
List<LatLng> routePointList = null;
GoogleMap googleMap = null;

private MapAnimator() {

}

public static MapAnimator getInstance() {
    if (mapAnimator == null) mapAnimator = new MapAnimator();
    return mapAnimator;
}

public void stopRouteAnim() {
    try {
        if (firstRunAnimSet != null) {
            firstRunAnimSet.removeAllListeners();
            firstRunAnimSet.end();
            firstRunAnimSet.cancel();
        }
        if (secondLoopRunAnimSet != null) {
            secondLoopRunAnimSet.removeAllListeners();
            secondLoopRunAnimSet.end();
            secondLoopRunAnimSet.cancel();
        }
        if (backgroundPolyline != null) {
            backgroundPolyline.remove();
            backgroundPolyline = null;
        }
        if (foregroundPolyline != null) {
            foregroundPolyline.remove();
            foregroundPolyline = null;
        }

        if (routePointList != null) {
            routePointList.clear();
        }
        if (googleMap != null) {
            googleMap = null;
        }

        if (optionsForeground != null) {
            optionsForeground = null;
        }
        MapAnimator.mapAnimator = null;
    } catch (Exception e) {

    }
}

public void animateRoute(GoogleMap googleMap, List<LatLng> routePointList, Context mContext) {
    this.mContext = mContext;
    this.routePointList = routePointList;
    this.googleMap = googleMap;
    if (firstRunAnimSet == null) {
        firstRunAnimSet = new AnimatorSet();
    } else {
        firstRunAnimSet.removeAllListeners();
        firstRunAnimSet.end();
        firstRunAnimSet.cancel();

        firstRunAnimSet = new AnimatorSet();
    }
    if (secondLoopRunAnimSet == null) {
        secondLoopRunAnimSet = new AnimatorSet();
    } else {
        secondLoopRunAnimSet.removeAllListeners();
        secondLoopRunAnimSet.end();
        secondLoopRunAnimSet.cancel();

        secondLoopRunAnimSet = new AnimatorSet();
    }
    //Reset the polylines
    if (foregroundPolyline != null) foregroundPolyline.remove();
    if (backgroundPolyline != null) backgroundPolyline.remove();

    PolylineOptions optionsBackground = new PolylineOptions().add(routePointList.get(0)).color(GREY).width(dipToPixels(mContext, 5));
    backgroundPolyline = googleMap.addPolyline(optionsBackground);

    optionsForeground = new PolylineOptions().add(routePointList.get(0)).color(Color.BLACK).width(dipToPixels(mContext, 5));
    foregroundPolyline = googleMap.addPolyline(optionsForeground);

    final ValueAnimator percentageCompletion = ValueAnimator.ofInt(0, 100);
    percentageCompletion.setDuration(1800);
    percentageCompletion.setInterpolator(new DecelerateInterpolator());
    percentageCompletion.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            if (backgroundPolyline == null || foregroundPolyline == null) {
                //   percentageCompletion.end();
                //  percentageCompletion.cancel();
                return;
            }
            List<LatLng> foregroundPoints = backgroundPolyline.getPoints();

            int percentageValue = (int) animation.getAnimatedValue();
            int pointcount = foregroundPoints.size();
            int countTobeRemoved = (int) (pointcount * (percentageValue / 100.0f));
            List<LatLng> subListTobeRemoved = foregroundPoints.subList(0, countTobeRemoved);
            subListTobeRemoved.clear();

            foregroundPolyline.setPoints(foregroundPoints);
        }
    });
    percentageCompletion.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (foregroundPolyline == null) {

                return;
            }
            foregroundPolyline.setColor(GREY);
            foregroundPolyline.setPoints(backgroundPolyline.getPoints());
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), GREY, Color.BLACK);
    colorAnimation.setInterpolator(new AccelerateInterpolator());
    colorAnimation.setDuration(15); // milliseconds

    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animator) {

            if (foregroundPolyline == null) {
                return;
            }
            foregroundPolyline.setColor((int) animator.getAnimatedValue());
        }

    });

    if (routePointList == null || routePointList.size() == 0) {
        return;
    }

    ObjectAnimator foregroundRouteAnimator = ObjectAnimator.ofObject(this, "routeIncreaseForward", new RouteEvaluator(), routePointList.toArray());
    foregroundRouteAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    foregroundRouteAnimator.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            if (backgroundPolyline == null) {
                return;
            }
            backgroundPolyline.setPoints(foregroundPolyline.getPoints());
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });
    foregroundRouteAnimator.setDuration(2500);

    firstRunAnimSet.playSequentially(foregroundRouteAnimator,
            percentageCompletion);
    firstRunAnimSet.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            secondLoopRunAnimSet.start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    secondLoopRunAnimSet.playSequentially(colorAnimation,
            percentageCompletion);
    secondLoopRunAnimSet.setStartDelay(10);

    secondLoopRunAnimSet.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            secondLoopRunAnimSet.start();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

    firstRunAnimSet.start();

}

/**
 * This will be invoked by the ObjectAnimator multiple times. Mostly every 16ms.

**/

public void setRouteIncreaseForward(LatLng endLatLng) {
    List<LatLng> foregroundPoints = foregroundPolyline.getPoints();
    foregroundPoints.add(endLatLng);
    foregroundPolyline.setPoints(foregroundPoints);
}

public class RouteEvaluator implements TypeEvaluator<LatLng> {
    @Override
    public LatLng evaluate(float t, LatLng startPoint, LatLng endPoint) {
        double lat = startPoint.latitude + t * (endPoint.latitude - startPoint.latitude);
        double lng = startPoint.longitude + t * (endPoint.longitude - startPoint.longitude);
        return new LatLng(lat, lng);
    }
}

public static int dipToPixels(Context context, float dipValue) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    return ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics));
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题