如何在Android Iconnenerator上居中文本

时间:2015-09-29 05:53:39

标签: android bitmap google-maps-markers google-maps-android-api-2 markerclusterer

我正在开发一个使用大量放置在地图上的标记的应用程序,我正在使用自定义ClusterRenderer来显示它们。 问题是我无法在自定义标记图标的中心绘制群集的大小,请参阅附带的屏幕截图。 我已经尝试将contentPadding添加到IconGenerator,但仍然没有运气,因为显示的位数变化。你能帮我把文字集中在生成的图标上吗?

代码:

 IconGenerator clusterIconGenerator = new IconGenerator(context);
 clusterIcon = context.getResources().getDrawable(R.drawable.map_cluster);
 clusterIconGenerator.setBackground(clusterIcon);

@Override
protected void onBeforeClusterRendered(Cluster<MyType> cluster, MarkerOptions markerOptions) {

    Bitmap clusterIcon = clusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(clusterIcon));
}

enter image description here

1 个答案:

答案 0 :(得分:15)

更新

从2016年4月1日开始,在库的资源中添加了一个前缀 所以id =“text”已更改为“amu_text”

如图书馆文件中所述:

  

setContentView public void setContentView(View contentView)
设置图标的子视图。
如果视图包含   TextView的id为“text”,操作如   setTextAppearance(Context,int)和makeIcon(String)将运行   在TextView上。

@Override
protected void onBeforeClusterRendered(Cluster<Dashboard_Marker> cluster, MarkerOptions markerOptions) {
    IconGenerator TextMarkerGen = new IconGenerator(context);
    Drawable marker;
    int ClusterSize = cluster.getSize();

    marker = context.getResources().getDrawable(R.drawable.cluster_red);
    TextMarkerGen.setBackground(marker);

    LayoutInflater myInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View activityView = myInflater.inflate(R.layout.cluster_view, null, false);

    TextMarkerGen.setContentView(activityView);
    TextMarkerGen.makeIcon(String.valueOf(cluster.getSize()));

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(TextMarkerGen.makeIcon());
    markerOptions.icon(icon);
}

布局cluster_view为:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerInParent="true"
    android:layout_centerVertical="true"
    android:weightSum="1">

    <TextView
        android:layout_width="61dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:textColor="#000000"
        android:id="@+id/text"
        android:layout_marginTop="13dp"
        android:gravity="center" />
</LinearLayout>

注意::布局必须包含一个带有id =“text”的文本视图,以便图标生成器接受它,操纵布局中所需的所有定位。