Android convertir vista XML a mapa de bits sin mostrarlo
Estoy tratando de establecer una vista de mi mapa de clúster. Estoy inflando una vista desde un XML y la configuración del texto de acuerdo al tamaño del clúster y quiero mostrar esa vista. En el código siguiente obtengo un mapa de bits nulo a cambio:
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 código siguiente obtengo el puntero nulo en la cuarta línea (los params de la disposición):
- Import xml en otro xml
- Java.lang.IllegalStateException: ScrollView puede alojar sólo un hijo directo
- ¿Tenemos alguna herramienta para convertir el diseño xml de Android a archivos .java?
- CollapsingToolbarLayout | Problemas de desplazamiento y diseño 2
- SimpleXML, org.simpleframework.xml.core.PersistenceException
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 cambiarlo al código siguiente no consigo el error pero nada se dibuja:
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; }
Este 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" />
- Colocación de mi ActionBar en la parte inferior
- No se pudo crear el contenido de la pestaña porque no se pudo encontrar la vista con id -1 Error XML en Android Eclipse?
- ScrollView no está jugando bien con RelativeLayout
- Creación / uso de XML de diseño generado en tiempo de ejecución en Android
- Cómo utilizar el icono de fuente (fuente-impresionante) en el selector de XML
- Cómo configurar el valor predeterminado del widget switch en android
- ¿Cómo alinear el botón a la derecha, sin ser superpuesto por TextView?
- Android: textColor del botón deshabilitado en el selector no se muestra?
Su cluster.getLayoutParams()
es probablemente null
. Primero, necesitas medir el ancho / alto de tu vista inflada y luego asignarle. Haz lo siguiente:
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; }
- Adición de tarros referenciados a android build.xml
- ¿Cuál es la regla del nombre de los paquetes que comienzan con el número?