2

オブジェクトで新しいsnapshot()メソッドを使用しています。SnapshotReadyCallbackでは、レイアウト内の を、コールバックから渡されたと交換しています。残念なことに、マップのレンダリングが完了する前にスナップショットが取得されたように見えます。GoogleMapMapViewImageViewBitmapBitmap

また、このビューはプログラムで作成されており、すぐにアクティビティに追加されないことも付け加えておく必要があります (実際には、他のビューの束と共に ScrollView に配置されています)。

私のコードの要点は(簡潔にするために)次のとおりです。

    public class MyCustomView extends LinearLayout {
      private FrameLayout mapContainer;

      @Override public void onAttachedToWindow() {
        super.onAttachedToWindow();

        // Setup GoogleMapOptions ...

        mapView = new MapView(getContext(), googleMapOptions);
        mapContainer.addView(mapView);
        mapView.onCreate(null);
        mapView.onResume();

        // Setup CameraUpdate ...

        mapView.getViewTreeObserver().addOnGlobalLayoutListener(new MapLayoutListener(mapView, cameraUpdate));
      }

      private class MapLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private final WeakReference<MapView> mapViewReference;
        private final CameraUpdate cameraUpdate;

        public MapLayoutListener(MapView mapView, CameraUpdate cameraUpdate) {
            mapViewReference = new WeakReference<MapView>(mapView);
            this.cameraUpdate = cameraUpdate
        }

        @Override public void onGlobalLayout() {
            MapView mapView = mapViewReference.get();
            if (mapView == null) {
                return;
            }
            mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            GoogleMap map = mapView.getMap();
            if (map == null) {
                return;
            }

            try {
                map.moveCamera(cameraUpdate);
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }

            map.snapshot(new GoogleMap.SnapshotReadyCallback() {
                @Override public void onSnapshotReady(Bitmap bitmap) {
                    MapView mapView = mapViewReference.get();
                    if (mapView == null) {
                        return;
                    }
                    mapContainer.removeAllViews();

                    ImageView imageView = new ImageView(getContext());
                    imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                    imageView.setImageBitmap(bitmap);
                    mapContainer.addView(imageView);
                }
            });
        }
    }
}

が呼び出されたときIllegalStateExceptionに がスローmap.moveCamera(cameraUpdate)され、ビューがまだレイアウトされていないと不平を言うことがありますがOnGlobalLayoutListener、ビューのレンダリング/レイアウトがすべて完了したときにコールバックを取得する適切な方法だと思った内で呼び出しています。この例外は私に別のことを考えさせます。

それで、これを行う適切な方法は何ですか?ドキュメントは詳細に乏しく、ビューの「ライフサイクル」はあまり明確ではありません。必要なのは、ビューを呼び出す準備ができたときのコールバックですsnapshot()

4

0 に答える 0