I have a code that takes the view that I pass as a parameter and transforms it into Bitmap (I do this to later throw that bitmap into a PDF file that I create.) The problem is that I create the view with a property of Width and Height and I realize that when I generate the bitmap these properties simply get lost ...
Here is the code I use:
//Função que transforma toda a activity em Bitmap
public static Bitmap getBitmapFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new RelativeLayout.LayoutParams(view.getMeasuredWidth(), view.getMeasuredHeight()));
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createScaledBitmap(view.getDrawingCache(),view.getMeasuredWidth(), view.getMeasuredHeight(), false);
view.setDrawingCacheEnabled(false);
return bitmap;
}