我想将RelativeLaout视图转换为Bitmap。我尝试了另一个答案和不同的选择但没有成功。 我的XML视图有点像:
----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内容是动态的,所以我不知道文本的宽度或高度。 除了例外,如果我在测量功能上手动设置足够的尺寸(输入为0),我的动态文本就不会完全显示。 我希望你能帮助我,因为现在,我卡住了。谢谢youuu
答案 0 :(得分:22)
尝试添加
v.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
在致电measure()
之前。这是否可以修复NullPointerException
?