measure() doesnt work properly with dynamic layouts and textView - Android

妖精的绣舞 提交于 2019-12-04 11:16:55

问题


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


回答1:


Try adding

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

before calling measure(). Does that fix the NullPointerException?



来源:https://stackoverflow.com/questions/5422607/measure-doesnt-work-properly-with-dynamic-layouts-and-textview-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!