所以我有一个单独的文件布局,所以我给它充气.然后我在我的布局中更改我的文本视图和图像,并调用measure,但是当我尝试测量布局时,我得到一个空指针.我无法为我的生活找出原因.我试图最终将布局转换为位图.
View inflatedView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.my_layout, null);
inflatedView.measure(getWidth(), getHeight());
inflatedView.layout(0, 0, inflatedView.getMeasuredWidth(),inflatedView.getMeasuredHeight());
Bitmap viewAsImage = Bitmap.createBitmap(inflatedView.getWidth(), inflatedView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas viewCanvas = new Canvas(viewAsImage);
Run Code Online (Sandbox Code Playgroud)
这是XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_gravity="bottom"
android:id="@+id/my_layout"
android:background="@color/transparent_black">
<ImageView android:id="@+id/image_left"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_centerVertical="true" android:layout_alignParentLeft="true"
android:scaleType="fitXY" android:maxWidth="48dip" android:maxHeight="48dip"
android:padding="5dip" />
<ImageView android:id="@+id/image_right"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginRight="20dip" android:src="@drawable/arrow"
android:layout_centerVertical="true" android:scaleType="fitXY"
android:maxWidth="48dip" android:maxHeight="48dip"
android:layout_alignParentRight="true" />
<TextView android:paddingLeft="4dip" android:paddingRight="1dip"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_left"
android:layout_toLeftOf="@id/image_right" android:id="@+id/some_name"
android:maxLines="1" android:ellipsize="end" android:textSize="20sp"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:textStyle="normal" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
在measure()行,它抛出一个空指针,我已经验证getWidth和getHeight给出了合法的值.
有任何想法吗?
谢谢!