1,RelativeLayout mPopView = (RelativeLayout) View.inflate(mContext, R.layout.show_pop, null);
private Bitmap convertViewToBitmap(RelativeLayout view) { view.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap; }在convertViewToBitMap()方法中加入
view.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
前,在view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),报NullPointException,加入那句后正常了。
在stackoverflow上得知
Inflater
does not extract layout_width
and layout_height
attributes when you don't assign its parent as an argument in inflate
method, so try to set LayoutParams
with specified width and height and then call measure.
参考:http://stackoverflow.com/questions/5940150/null-pointer-exception-when-calling-measure-on-inflated-layout