8

我想将 RelativeLaout 视图转换为位图。我尝试了另一个答案和不同的选择,但没有成功。我的 XML 视图是这样的:

----相对布局
--------ImageView
--------文本视图

每个人都有 wrap_content 措施。

因为我需要视图的几个位图,所以我用这种方式膨胀它:

  LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
查看 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);
返回 v;

在那之后 :

//在 measure() 中,我在 RelativeLayout.onMeasure() 上得到一个 nullpointerException。我也尝试过 //with MeasureSpec.EXACTLY
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
位图 b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
v.draw(新画布(b));
油漆 p = 新油漆();
myMainCanvas.drawBitmap(b, myPointX, myPointY, p);                   

RelativeLayout 的 textView 的内容是动态的,所以我无法知道文本的宽度或高度。除了例外,如果我在测量函数上手动设置足够的大小(设置为 0),我的动态文本不会完全显示。我希望你能帮助我,因为现在,我卡住了。谢谢你

4

1 回答 1

22

尝试添加

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

打电话之前measure()。这能解决问题NullPointerException吗?

于 2011-05-22T06:53:35.017 回答