使用android xml在运行时创建自定义标记图标 [英] create custom marker icons on runtime using android xml

查看:264
本文介绍了使用android xml在运行时创建自定义标记图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,在该应用程序中,我需要从其URL下载图像,从中创建一个位图,然后将其作为图标显示在地图上.到目前为止,我已经成功完成了在地图上动态显示位图的操作,但是需要将图像放置在某些背景上.此处显示的类似内容如何使用Android创建自定义形状的位图标记地图API v2 ? 任何示例或建议都会有所帮助. 谢谢...:)

I am developing an android app in which I need to download an image from its URL, create a bitmap out of it and than display it on map as an icon. so far I have successfully completed to show bitmaps dynamically on map but I need to place my images on some background. something like this shown here How to create a custom-shaped bitmap marker with Android map API v2? any example or suggestion would be helpful. thanks...:)

推荐答案

上述问题我都得到了答案 我从视图创建了一个位图,然后将其放置在标记上.我的xml在下面

I got the answer for my above problem I created a bitmap from view and then placed it on marker. my xml is below

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
    <ImageView
    android:id="@+id/img_id"
    android:layout_width="@dimen/custom_profile_image"
    android:layout_height="@dimen/custom_profile_image"
    android:contentDescription="@string/content"
    android:scaleType="centerCrop" />
    </RelativeLayout>

在xml上方夸大

    View viewMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                .inflate(R.layout.custom_marker_layout, null);
    ImageView myImage = (ImageView) viewMarker.findViewById(R.id.img_id);

在运行时从url下载的imageview上设置Imagebitmap

set Imagebitmap on imageview which you download from url on runtime

    myImage.setImageBitmap(Imagebitmap);

现在将视图对象传递给该方法,并从中返回位图.像这样

now pass view object to the method and return the bitmap out of it. like this

    Bitmap bmp=createDrawableFromView(context,viewMarker);

    googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude)).title(title)
            .snippet(snippet)
            .icon(BitmapDescriptorFactory.fromBitmap(bmp)));

现在您可以在任何地方使用返回的位图,那就可以使用xml制作自定义标记图标.

now you can use returned bitmap anywhere, thats you'll be able to make custom marker icons using xmls.

    public static Bitmap createDrawableFromView(Context context, View view) {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay()
            .getMetrics(displayMetrics);
    view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT));
    view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
    view.layout(0, 0, displayMetrics.widthPixels,
            displayMetrics.heightPixels);
    view.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
            view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

它为我工作.谢谢!!!

Its working for me. Thanks!!!

这篇关于使用android xml在运行时创建自定义标记图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆