How can I convert ViewGroup into white background Bitmap?

99 views Asked by At
        view.measure(MeasureSpec.makeMeasureSpec(view.layoutParams.width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(view.layoutParams.height, MeasureSpec.EXACTLY))
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)
        return Bitmap.createBitmap(view.width, view.height, Bitmap.Config.RGB_565)

And I got

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.setHasAlpha(boolean)' on a null object reference

The part that I want to convert is rl_logo_name

       <RelativeLayout
            android:id="@+id/rl_logo_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="42px"
            android:layout_marginRight="30px"
            android:layout_marginTop="35px">

            <!-- The source file is vector(xml) file -->
            <ImageView
                android:id="@+id/iv_logo"
                android:layout_width="81px"
                android:layout_height="42px"
                android:layout_marginTop="29px"
                android:background="@drawable/ico_logo"
                android:textStyle="bold" />

            <RelativeLayout
                android:id="@+id/rl_name"
                android:layout_width="wrap_content"
                android:layout_height="72px"
                android:layout_marginLeft="3px"
                android:layout_toRightOf="@id/iv_logo">

                <TextView
                    android:id="@+id/tv_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="John"
                    android:textSize="72px"
                    android:textColor="#323232"
                    android:paddingLeft="51px"
                    android:paddingRight="51px"/>
            </RelativeLayout>
        </RelativeLayout>

rl_logo_name's parent view is RelativeLayout. I want them to be converted to a one bitmap with a white background. How can I achieve that?

1

There are 1 answers

0
Mahmoud Afarideh On

Add these to your code:

val c = Canvas(bitmap)
v.draw(c)
return bitmap