Tôi đang cố gắng đặt chế độ xem cho cụm bản đồ của mình. Tôi đang tăng lượt xem từ XML và đặt Văn bản theo kích thước cụm và tôi muốn hiển thị chế độ xem đó. trong đoạn mã sau tôi nhận được một bitmap null trong trở lại:android chuyển đổi XML Xem sang Bitmap mà không hiển thị nó
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;
}
trong đoạn mã sau tôi nhận được con trỏ null trên dòng thứ tư (cách bố trí params):
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;
}
và khi thay đổi nó để đoạn mã sau tôi nhận được không lỗi nhưng không được drawed:
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;
}
này là XML của tôi:
<?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" />
tìm thấy giải pháp trong [này] [1] câu trả lời [1]: http://stackoverflow.com/questions/2339429/android-view-getdrawingcache- trả về-null-only-null/4618030 # 4618030 –