我正在尝试将一个视图设置到我的地图聚合中。
我从XML中填充了一个视图,并根据聚合大小设置了文本,我想展示这个视图。
在下面的代码中,我得到一个空位图作为返回值:
在下面的代码中,我在第四行(即布局参数)处得到了空指针异常:
这是我的XML文件:
private Bitmap createClusterBitmap(int clusterSize) {
View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);
cluster.setText(String.valueOf(clusterSize));
cluster.setDrawingCacheEnabled(true);
cluster.buildDrawingCache(true);
Bitmap bm = cluster.getDrawingCache();
return bm;
}
在下面的代码中,我在第四行(即布局参数)处得到了空指针异常:
private Bitmap createClusterBitmap(int clusterSize) {
View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);
TextView clusterSizeText = (TextView) cluster.findViewById(R.map.cluster);
clusterSizeText.setText(String.valueOf(clusterSize));
Bitmap clusterBitmap = Bitmap.createBitmap( cluster.getLayoutParams().width, cluster.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas clusterCanvas = new Canvas(clusterBitmap);
cluster.layout(cluster.getLeft(), cluster.getTop(), cluster.getRight(), cluster.getBottom());
cluster.draw(clusterCanvas);
return clusterBitmap;
}
当我把它改为以下代码时,没有错误但是没有画出任何东西:
private Bitmap createClusterBitmap(int clusterSize) {
View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);
TextView clusterSizeText = (TextView) cluster.findViewById(R.map.cluster);
clusterSizeText.setText(String.valueOf(clusterSize));
Bitmap clusterBitmap = Bitmap.createBitmap( 50,50 , Bitmap.Config.ARGB_8888);
Canvas clusterCanvas = new Canvas(clusterBitmap);
cluster.layout(50, 50, 50, 50;
cluster.draw(clusterCanvas);
return clusterBitmap;
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+map/cluster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/map_pointer_cluster"
android:gravity="center"
android:orientation="vertical"
android:textColor="@android:color/black"
android:textSize="35dp"
android:textStyle="bold" />