CRO*_*M87 8 android bitmap dynamic textview measure
我想将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
tha*_*kis 22
尝试添加
v.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Run Code Online (Sandbox Code Playgroud)
之前调用measure().这样可以解决NullPointerException吗?