measure() 无法与动态布局和 textView 一起正常工作 - Android
我想将RelativeLaout 视图转换为位图。我尝试了其他答案和不同的选项,但没有成功。 我的 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),我的动态文本不会完全显示。 我希望你能帮助我,因为现在我被困住了。谢谢你uu
I want to convert a RelativeLaout view into a Bitmap. I've tried another answers and different options without success.
My XML View is kind of:
----RelativeLayout -------ImageView -------TextView
Every ones have wrap_content measures.
As i need several bitmaps of the view, im inflating it this way:
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;
After that :
//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);
The content of textView of the RelativeLayout is dynamic, so i cant know the width or height of the text.
Besides of the exception, if i manually set enough size on the measure function (insted of 0), my dynamic text doesnt display entirely.
I hope you can help me because right now, im stuck. Thank youuu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
在调用
measure()
之前添加。这是否可以解决 NullPointerException 问题?Try adding
before calling
measure()
. Does that fix theNullPointerException
?