2011-03-24 19 views
8

我想將RelativeLaout視圖轉換爲位圖。我嘗試了另一個答案和不同的選項,但沒有成功。 我的XML觀是怎麼樣的:measure()不適用於動態佈局和textView - Android

 
----RelativeLayout 
-------ImageView 
-------TextView 

每個那些已經WRAP_CONTENT措施。

因爲我需要考慮的幾個位圖,即時充氣這樣說:

 LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = inflater.inflate(R.layout.localviewlayout, null); 

ImageView img = (ImageView) v.findViewById(R.id.imgPlace); 
img.setImageBitmap(MyDynamicBitmap); 

TextView txt1 = (TextView)v.findViewById(R.id.text1); 
txt1.setText(MyDynamicText); 
return v;

之後:

 

//in measure() i get a nullpointerException on RelativeLayout.onMeasure().I also have tried //with MeasureSpec.EXACTLY 
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
          MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); 
v.draw(new Canvas(b)); 
Paint p = new Paint(); 
myMainCanvas.drawBitmap(b, myPointX, myPointY, p);     

中的RelativeLayout的TextView中的內容是動態的,所以我不能知道文字的寬度或高度。 除了例外,如果我手動設置足夠大小的度量函數(insted 0),我的動態文本並不完全顯示。 我希望你能幫助我,因爲現在,我卡住了。謝謝youuu

回答

22

嘗試調用measure()前加入

v.setLayoutParams(new LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

。這是否解決了NullPointerException

+0

與OP有相同的問題,並從這篇文章中添加setLayoutParams修復了空指針異常。謝謝! – 2012-05-06 19:47:24

+0

謝謝!你救了我! – 2016-03-24 11:35:32