Android API 31中的片段布局截图



在API 31之前,在Android中截取片段布局的截图非常容易。我只是用了下面的代码。

val mBitmap = Bitmap.createBitmap(binding.root.width, binding.root.height, Bitmap.Config.ARGB_8888)
binding.root.draw(Canvas(mBitmap))

使用上面的代码,布局中的所有视图都被绘制到画布上。问题在于,由于API 31,似乎只绘制了布局的直接子视图,而没有绘制间接子视图(布局的某些子Viewgroups内部的视图(。我现在应该一个接一个地画出所有的视图,还是有一些更简单的方法?

好的,我发现了API 31的问题。我想截图的布局如下

<FrameLayout
android:id="@+id/poet_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/poet_coordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<androidx.core.widget.NestedScrollView
android:id="@+id/alaki"
android:layout_width="match_parent"
android:layout_height="2000dp"
android:fitsSystemWindows="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/shelf_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="false"
android:paddingBottom="30dp"
android:scrollbars="none"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/shelf_layout"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/poet_book_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="false"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="30dp"
android:scrollbars="none"
android:theme="@style/DefaultRecyclerViewStyle"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/book_item"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/widgets"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/molding"
android:layout_width="match_parent"
android:layout_height="0dp"
android:tintMode="src_atop"
android:scaleType="fitXY"
tools:srcCompat="@drawable/molding2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.15" />
<ImageView
android:id="@+id/molding_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:tintMode="src_atop"
android:scaleType="fitXY"
tools:srcCompat="@drawable/molding_bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.07"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="80dp"
app:toolbarTitle="@{item.text}"
app:titleTextColor="?attr/colorOnSurface"
app:menu="@menu/poet_frag_menu"
app:layout_collapseMode="pin"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<ImageView
android:id="@+id/wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:translationZ="-1dp"
tools:srcCompat="@drawable/wall1"/>
</FrameLayout>

正如您所看到的,布局底部有一个ImageViewid="@+id/wall"填充了整个布局。我将translationZ="-1dp"设置为将其发送到布局的背景。在API 31之前一切正常,所有视图都绘制到画布上。但API 31似乎无法意识到该视图落后于其他视图,并将其绘制在所有其他视图之上。为了解决这个问题,我把这个ImageView放在了布局的顶部。修正后的布局变成这样,

<FrameLayout
android:id="@+id/poet_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/wall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:translationZ="-1dp"
tools:srcCompat="@drawable/wall1"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/poet_coordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<androidx.core.widget.NestedScrollView
android:id="@+id/alaki"
android:layout_width="match_parent"
android:layout_height="2000dp"
android:fitsSystemWindows="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/shelf_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="false"
android:paddingBottom="30dp"
android:scrollbars="none"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/shelf_layout"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/poet_book_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fitsSystemWindows="false"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingBottom="30dp"
android:scrollbars="none"
android:theme="@style/DefaultRecyclerViewStyle"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/book_item"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/widgets"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/molding"
android:layout_width="match_parent"
android:layout_height="0dp"
android:tintMode="src_atop"
android:scaleType="fitXY"
tools:srcCompat="@drawable/molding2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.15" />
<ImageView
android:id="@+id/molding_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:tintMode="src_atop"
android:scaleType="fitXY"
tools:srcCompat="@drawable/molding_bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="0.07"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="80dp"
app:toolbarTitle="@{item.text}"
app:titleTextColor="?attr/colorOnSurface"
app:menu="@menu/poet_frag_menu"
app:layout_collapseMode="pin"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

最新更新