Hello everyone!
Make the outline of a collage like this:
Which shows the images correctly, the point is that I'm interested in saving that set of imageViews in a single image for better management, but I do not see how to do it.
After a few days I was able to solve my problem satisfactorily. I share what I found:
To create a bitmap resource from an element already defined from the xml, just put:
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
* view is the element that will be converted to image
My problem was that the collage filled dynamically every time they accessed the window, so the previous code did not work for me, however I found an alternative:
In this case the photobooks_05_collage layout was what the content of my view had (collage) It was enough to add a Layout at the beginning that will have the container function, with properties adjustable to the content:
RelativeLayout viewRoot = new RelativeLayout(getContext());
View v = inflater.inflate(R.layout.photobooks_05_collage, viewRoot, true);
viewRoot.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
//Aquí hice la logica y asignación de imagenes al collage, poblando cada ImageView correspondiente.
//Con esta sentencia se evita que el contenedor tenga valores height y width nulos.
viewRoot.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//Se asigna un tamaño a la vista contenedora
viewRoot.layout(0, 0, viewRoot.getMeasuredWidth(), viewRoot.getMeasuredHeight());
//Se crea el bitmap
final Bitmap bitmap=Bitmap.createBitmap(viewRoot.getMeasuredWidth(),
viewRoot.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
//Renderiza la vista del contenedor
viewRoot.draw(c);
The bitmap object is the container of the final image
This is enough to convert any design of views, however complex, into an image.
Reference (in English): link