Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android converting XML View to Bitmap without showing it

i'm trying to set a view to my map cluster. i'm inflating a view from an XML and setting the Text according to cluster size and i want to show that view. in the following code i get a null bitmap in return:

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

in the following code i get null pointer on the fourth line (the layout 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;
}

and when changing it to the following code i get not error but nothing is 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;
}

this is my 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" />
like image 701
Asaf Nevo Avatar asked Sep 13 '12 08:09

Asaf Nevo


1 Answers

Your cluster.getLayoutParams() is probably null. First, you need to measure the width/height of your inflated view and then assign to it. Do it as below:

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;
}
like image 142
waqaslam Avatar answered Oct 25 '22 17:10

waqaslam