2012-09-13 19 views
7

Tôi đang cố gắng đặt chế độ xem cho cụm bản đồ của mình. Tôi đang tăng lượt xem từ XML và đặt Văn bản theo kích thước cụm và tôi muốn hiển thị chế độ xem đó. trong đoạn mã sau tôi nhận được một bitmap null trong trở lại:android chuyển đổi XML Xem sang Bitmap mà không hiển thị nó

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

trong đoạn mã sau tôi nhận được con trỏ null trên dòng thứ tư (cách bố trí 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; 
} 

và khi thay đổi nó để đoạn mã sau tôi nhận được không lỗi nhưng không được 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; 
} 

này là XML của tôi:

<?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

tìm thấy giải pháp trong [này] [1] câu trả lời [1]: http://stackoverflow.com/questions/2339429/android-view-getdrawingcache- trả về-null-only-null/4618030 # 4618030 –

Trả lời

21

cluster.getLayoutParams() của bạn có thể là null. Trước tiên, bạn cần phải đo chiều rộng/chiều cao của chế độ xem được tăng cao và sau đó gán cho nó. Làm điều đó như sau:

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 đó là lỗi đánh máy. cảm ơn vì đã chỉ ra :) – waqaslam

Các vấn đề liên quan