动画与折线无法正常工作?

时间:2018-03-19 20:16:10

标签: android google-maps android-animation google-polyline

我正在创建polyline animation&正在创建onLocationChanged() polyline。每次调用onLocationChanged()时,都会创建一个新的polyline

Note:动画如下:首先黑色线将移至源到目标 animation然后灰色线即可。问题是,它有时候工作正常。但有一段时间后,黑线不可见,只显示灰线动画。

供参考,请参阅附件。在gif中,您可以看到它在前四次时工作正常,之后您可以注意到,黑线不可见。它是隐形的,因为它在灰线后面。

enter image description here

这是我的onLocationChanged()方法:

    @Override
    public void onLocationChanged(final Location location) {
        super.onLocationChanged(location);
        mLat = location.getLatitude();
        mLng = location.getLongitude();

        LatLng myLatLngCurrentONlocationChnage = new LatLng(mLat, mLng);

        if (btnYesBooking || isBookingReserve) {
            mSearchComplete.setVisibility(View.GONE);
            String url = getDirectionsUrl(myLatLngCurrentONlocationChnage, myLatLngFinalDestinationClicked);

            DownloadTask downloadTask = new DownloadTask();

            // Start downloading json data from Google Directions API
            downloadTask.execute(url);

            myDestinationBookingMarker = mGoogleMap.addMarker(new MarkerOptions()
                    .position(myLatLngFinalDestinationClicked)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.red_pin)));
            addMarker(mGoogleMapCar, mLat, mLng);
}

请查看 onPostExecute()方法,其中动画代码已实现。

        @Override
        protected void onPostExecute(List<List<HashMap<String, String>>> result) {

            PolylineOptions lineOptions = null;
            if (result != null) {
                // Traversing through all the routes
                for (int i = 0; i < result.size(); i++) {
                    points = new ArrayList<LatLng>();
                    lineOptions = new PolylineOptions();

                    // Fetching i-th route
                    final List<HashMap<String, String>> path = result.get(i);

                    // Fetching all the points in i-th route
                    for (int j = 0; j < path.size(); j++) {
                        HashMap<String, String> point = path.get(j);
                        if (j == 0) {    // Get distance from the list
                            estimateDistance = (String) point.get("distance");

                            continue;
                        } else if (j == 1) { // Get duration from the list
                            estimeteDuration = (String) point.get("duration");
                            continue;
                        }

                        double lat = Double.parseDouble(point.get("lat"));
                        double lng = Double.parseDouble(point.get("lng"));
                        LatLng position = new LatLng(lat, lng);
                        // points.clear();
                        points.add(position);
                    }


                    if (estimeteDuration != null) {
                        mTvEstimatedTime.setText("Estimated Time to reach: " + estimeteDuration);
                        mSearchComplete.setVisibility(View.GONE);
                        mTxtEstimateArrivalTime.setText(estimeteDuration);


                    }
                    mTvEstimatedTime.setVisibility(View.VISIBLE);
                    mTxtEstimateArrivalTime.setVisibility(View.VISIBLE);
                    mRecyclerViewHotspot.setVisibility(View.GONE);
                    mDetailLayout.setVisibility(View.GONE);
                    mParkingdetailLayout.setVisibility(View.GONE);

                    lineOptions.addAll(points);
                }

                LatLngBounds.Builder builder = new LatLngBounds.Builder();
                for (LatLng latLng : points) {
                    builder.include(latLng);
                }
                polylineOptions = new PolylineOptions();
                polylineOptions.color(Color.GRAY);
                polylineOptions.width(12);
                polylineOptions.startCap(new SquareCap());
                polylineOptions.endCap(new SquareCap());
                polylineOptions.jointType(ROUND);
                polylineOptions.addAll(points);
                greyPolyLine = mGoogleMap.addPolyline(polylineOptions);

                blackPolylineOptions = new PolylineOptions();
                blackPolylineOptions.width(12);
                blackPolylineOptions.color(Color.BLACK);
                blackPolylineOptions.startCap(new SquareCap());
                blackPolylineOptions.endCap(new SquareCap());
                blackPolylineOptions.jointType(ROUND);

                blackPolyline = mGoogleMap.addPolyline(blackPolylineOptions);
                polylineAnimator = ValueAnimator.ofInt(0, 100);
                polylineAnimator.setDuration(700);
                polylineAnimator.setInterpolator(new LinearInterpolator());
                polylineAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {

                        List<LatLng> points = greyPolyLine.getPoints();
                        int percentValue = (int) valueAnimator.getAnimatedValue();
                        int size = points.size();
                        int newPoints = (int) (size * (percentValue / 100.0f));
                        List<LatLng> p = points.subList(0, newPoints);
                        blackPolyline.setPoints(p);


                    }
                });


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

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        List<LatLng> greyLatLng = greyPolyLine.getPoints();
                        if (greyLatLng != null) {
                            greyLatLng.clear();

                        }
                        polylineAnimator.start();

                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {
                        polylineAnimator.cancel();
                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });

                polylineAnimator.start();


            }
            mGoogleMap.getUiSettings().setZoomGesturesEnabled(false);
            zoomRoute(mGoogleMap, points);

        }

知道我做错了什么吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以为黑线设置zIndex,以确保在灰线上绘制。

the documentation中所述:

  

具有较大z-index的叠加层将覆盖较小的叠加层   z折射率。具有相同z-index的叠加的顺序是任意的。   默认的zIndex为0。

您可以使用blackPolylineOptions对象设置行的zIndex:

blackPolylineOptions.zIndex(5f); // Using 5 as an example, anything greater than 0 (the default) will work