将位图图像舍入为谷歌地图标记图标

时间:2017-11-19 23:33:43

标签: java android google-maps parsing bitmap

我有一个地图活动,可以从解析服务器检索图像和位置 并在地图上显示它们作为标记,我想知道我是否可以使这些标记圆形而不是矩形形状。

it looks like this now

这是我的代码:

public void getimagesLocation(){

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Image");
    ParseGeoPoint geoPointLocation = new ParseGeoPoint();
    query.whereNear("location", geoPointLocation);

    query.setLimit(100);
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Log.i("imageslocation", "no errors");}
            if (objects.size() > 0) {
                for (ParseObject object : objects) {
                    final ParseGeoPoint point = object.getParseGeoPoint("location");
                    ParseFile file = (ParseFile) object.get("image");
                    file.getDataInBackground(new GetDataCallback() {
                        @Override
                        public void done(byte[] data, ParseException e) {
                            if (e == null && data != null);

                            Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
                            Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap,200,200);

                            Double lat = point.getLatitude();
                            Double log = point.getLongitude();

                            LatLng marker = new LatLng(lat, log);

                            mMap.addMarker(new MarkerOptions()
                                    .title("Another Image")
                                    .icon(BitmapDescriptorFactory.fromBitmap(thumbnail))
                                    .position(marker));

                        }});}}}});}

那么,如何使这些标记圆形而不是矩形?

2 个答案:

答案 0 :(得分:1)

我认为问题在于图像资产。您应该使用透明* .png图片格式

像这样:

enter image description here

答案 1 :(得分:0)

好吧我发现了一个解决方案,如果有人感兴趣的话,

 public Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

然后简单地使用它来提取缩略图

Bitmap thumbnail = ThumbnailUtils.extractThumbnail(getCroppedBitmap(bitmap),200,200);