简体   繁体   English

如何在Android谷歌地图中制作自定义瓷砖

[英]How to make custom Tiles in android google maps

Android's Google Maps api v2 provides a TileOverlay to which TileProviders can be added. Android的Google Maps api v2提供了TileOverlay,可以添加TileProviders。 A TileProvider will generate a Tile object given the lat, long, and depth. 给定lat,long和depth,TileProvider将生成Tile对象。 In order to make a tile, one must give it width (easy), height (easy), and an image represented as a byte array (confusing me). 为了制作瓷砖,必须给它宽度(简单),高度(简单)和表示为字节数组的图像(使我感到困惑)。 If I wanted to 'draw' a simple object and then turn it into a byte array, how would I do this? 如果我想“绘制”一个简单的对象,然后将其转换为字节数组,我该怎么做?

For instance I am looking for something that looks like 例如,我正在寻找一些看起来像的东西

Canvas canvas = new canvas();
...
canvas.drawRect(); //Or something like this (just an example)
...
byte[] bytes = canvas.SomeConversionFunctionOrProcessThatIDontKnow();
return new Tile(1,1,bytes);
public byte[] getByteArray (String image) throws IOException {
 File yourImg = new File(image);
 BufferedImage bufferedImage = ImageIO.read(yourImg);
 WritableRaster wRaster = bufferedImage .getRaster();
 DataBufferByte data   = (DataBufferByte) wRaster.getDataBuffer();

 return data.getData();
}

This should do the trick ; 这应该可以解决问题; )

Tile#data should be compressed image data in one of the supported image formats . Tile#data应该是支持的图像格式之一的压缩图像数据。 In other words, the raw contents of an image file. 换句话说,图像文件的原始内容。 If you already have a decoded Bitmap , use Bitmap#compress(...) to write it to a ByteArrayOutputStream , then get the byte[] from that. 如果您已经有解码的Bitmap ,请使用Bitmap#compress(...)将其写入ByteArrayOutputStream ,然后从中获取byte[]

Bitmap bitmap = Bitmap.createBitmap(TILE_DIMENSION, TILE_DIMENSION,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
......
//draw something
......
Tile tile = convertBitmap(bitmap);
bitmap.recycle();
return tile;
}

private Tile convertBitmap(Bitmap bitmap){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] bitmapData = stream.toByteArray();

    return new Tile(TILE_DIMENSION, TILE_DIMENSION, bitmapData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM