措施()不工作正确地与动态布局和TextView中 - 安卓 [英] measure() doesnt work properly with dynamic layouts and textView - Android
问题描述
我想转换一个RelativeLaout视图成位图。我试过另一种答案,不同的选择没有成功。 我的XML观是怎么样的:
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
每个那些已经WRAP_CONTENT措施。
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;
之后:
//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
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
推荐答案
尝试添加
v.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
之前调用措施()
。这是否解决了 NullPointerException异常
?
这篇关于措施()不工作正确地与动态布局和TextView中 - 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!