2012-09-13 31 views
7

我想設置一個視圖到我的地圖集羣。 我正在從一個XML膨脹視圖,並根據羣集大小設置文本,我想要顯示該視圖。 在下面的代碼我得到一個空的位圖中的返回:android將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; 
} 
在下面的代碼

我得到在第四行空指針(佈局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; 
} 

,並將其改變爲當下面的代碼我得到沒有錯誤,但沒有什麼是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; 
} 

這是我的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" /> 
+0

發現[這裏] [1]的答案解決方案 [1]:http://stackoverflow.com/questions/2339429/android-view-getdrawingcache- returns-null-only-null/4618030#4618030 –

回答

21

您的cluster.getLayoutParams()可能是null。首先,您需要測量充氣視圖的寬度/高度,然後分配給它。做到這一點,如下:

private Bitmap createClusterBitmap(int clusterSize) { 
    View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, 
      null); 

    TextView clusterSizeText = (TextView) cluster.findViewById(R.id.map_cluster_text); 
    clusterSizeText.setText(String.valueOf(clusterSize)); 

    cluster.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    cluster.layout(0, 0, cluster.getMeasuredWidth(),cluster.getMeasuredHeight()); 

    final Bitmap clusterBitmap = Bitmap.createBitmap(cluster.getMeasuredWidth(), 
      cluster.getMeasuredHeight(), Bitmap.Config.ARGB_8888); 

    Canvas canvas = new Canvas(clusterBitmap); 
    cluster.draw(canvas); 

    return clusterBitmap; 
} 
+0

R.map.cluster ???? – Hamidreza

+2

@Hamidreza這是一個錯字。感謝指出:) – waqaslam

相關問題