2012-09-13 16 views
7

मैं अपने मानचित्र क्लस्टर पर एक दृश्य सेट करने की कोशिश कर रहा हूं। मैं एक एक्सएमएल से एक दृश्य फुला रहा हूं और क्लस्टर आकार के अनुसार पाठ सेट कर रहा हूं और मैं उस दृश्य को दिखाना चाहता हूं। निम्नलिखित कोड में मैं बदले में एक अशक्त बिटमैप मिलती है:एंड्रॉइड कनवर्ट करना एक्सएमएल व्यू को बिटमैप में दिखाए बिना

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; 
} 
निम्नलिखित कोड में

मैं चौथी लाइन पर नल पॉइंटर (लेआउट पैरामीटर) मिलती है:

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 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- रिटर्न-नल-ओन-नल/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

आर.मैप.क्लस्टर ???? – Hamidreza

+2

@ हैमिड्रेज़ा यह एक टाइपो था। इंगित करने के लिए धन्यवाद :) – waqaslam

संबंधित मुद्दे