2012-09-13 21 views
7

estoy tratando de establecer una vista para mi clúster de mapas. estoy inflando una vista desde un XML y configurando el texto de acuerdo con el tamaño del clúster y quiero mostrar esa vista. en el siguiente código me sale un mapa de bits nulos a cambio:android convertir vista XML a mapa de bits sin mostrarlo

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; 
} 

en el siguiente código me sale puntero nulo en la cuarta línea (el params diseño):

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; 
} 

y al cambiar a el siguiente código no da error pero nada es dibujé:

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; 
} 

esta es mi 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

encontró la solución en [esta] [1] respuesta [1]: http://stackoverflow.com/questions/2339429/android-view-getdrawingcache- returns-null-only-null/4618030 # 4618030 –

Respuesta

21

Su cluster.getLayoutParams() es probablemente null. Primero, necesita medir el ancho/alto de su vista inflada y luego asignarla. Hacerlo de la siguiente manera:

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 fue un error tipográfico. gracias por señalar :) – waqaslam

Cuestiones relacionadas