I’m having issues converting layout to Bitmap keeping the correct dimensions.
This is what I have in my layout file:
And this is what I get as the result:
It creates the wrong dimensions, and seems that gravity for the text is ignored.
(I’m creating a Bitmap to add as google maps Marker)
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/street_label"
android:layout_width="40dip"
android:layout_height="30dip"
android:background="@mipmap/ic_marker_middle">
<TextView
android:id="@+id/label_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="3dip"
android:gravity="center"
android:text=""
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold"
tools:text="G" />
</FrameLayout>
And my Java code:
private Bitmap createStreetLabel(String labelText) {
View cluster = LayoutInflater.from(context).inflate(R.layout.street_label,
null);
TextView clusterSizeText = (TextView) cluster.findViewById(R.id.label_text);
clusterSizeText.setText(labelText);
cluster.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
cluster.layout(0, 0, cluster.getMeasuredWidth(),cluster.getMeasuredHeight());
final Bitmap bitmap = Bitmap.createBitmap(cluster.getMeasuredWidth(),
cluster.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
cluster.draw(canvas);
return bitmap;
}