Сохранить mapView как изображение, не отображая его на экране - PullRequest
0 голосов
/ 11 сентября 2018

Я создаю бесплатное приложение для мониторинга активности, похожее на Strava.Использование vtm-android в качестве библиотеки карт.

Я бы хотел сохранить org.oscim.android.MapView в качестве изображения, не отображая его на экране.

Я нашел несколько решений, как сохранить вид какизображения, но, к сожалению, ни один из них не работает, когда представление не «привязано» к существующему дереву компонентов.

Я ожидал инициализировать MapView следующим образом:

final String MAP_FILE = "somemap.map";
MapView mapView = new MapView(mContext);

final org.oscim.map.Map map = mapView.map();
MapFileTileSource tileSource = new MapFileTileSource();

String mapPath = new File(Environment.getExternalStorageDirectory(), MAP_FILE).getAbsolutePath();
if (tileSource.setMapFile(mapPath)) {
    // Vector layer
    VectorTileLayer tileLayer = map.setBaseMap(tileSource);

    // Render theme
    map.setTheme(VtmThemes.DEFAULT);

    double latitude = 49.1625214;
    double longitude = 20.2644075;

    // Note: this map position is specific to Berlin area
    map.setMapPosition(latitude, longitude, 1 << 12);
}

mapView.layout(0, 0, 200, 200);

//Get the dimensions of the view so we can re-layout the view at its current size
//and create a bitmap of the same size
int width = view.getWidth();
int height = view.getHeight();

int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

//Cause the view to re-layout
view.measure(measuredWidth, measuredHeight);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

//Create a bitmap backed Canvas to draw the view into
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

//Now that the view is laid out and we have a canvas, ask the view to draw itself into the canvas
view.draw(canvas);

ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(byteBuffer);

//save bytes from bytebuffer to file or db

Но вместо картыизображение, я получаю полностью прозрачное изображение.

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018

mapView.getDrawingCache ()

Он вернет вам растровое изображение текущего отображения.

Растровое изображение capturedScreen = Bitmap.createBitmap (mapView.getDrawingCache ()); imageView.setImageBitmap (capturedScreen);

Здесь «capturedScreen» даст вам изображение карты.

0 голосов
/ 11 сентября 2018

Чтобы сделать снимок экрана, вам нужно сделать его на экране.Без рендеринга скриншот невозможен

...