我正在尝试从未在 UI 上显示的视图创建位图。
在我看来有 recyclerview。我不明白为什么 recyclerview 的高度会变大。
这里是视图xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_order_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="5dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="5dp"
android:text="OrderID: 45453w"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_store_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:text="Google-Apple"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_store_location"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:text="120 Grant Ave. #8 Mountain view, CA 34343"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_customer_namee"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="5dp"
android:gravity="start"
android:text="Customer: John Smith"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_order_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:gravity="start"
android:text="Order time: Jan 21, 2021, 3:34PM"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_pickup_time"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:gravity="start"
android:text="Pickup time: Jan 21, 2021, 3:34PM"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_order_items1"
android:layout_width="match_parent"
android:background="@color/red"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_subtotal"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/subtotal"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_subtotal_price"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="32dp"
android:gravity="start"
android:text="125$"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
</LinearLayout>
<LinearLayout
android:id="@+id/discount_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp">
<TextView
android:id="@+id/tv_total_discount"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/payment_total_discount"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_total_discount_price"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="32dp"
android:gravity="start"
android:text="15$"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp">
<TextView
android:id="@+id/tv_total_tax"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/tax"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_total_tax_price"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="32dp"
android:gravity="start"
android:text="10$"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="@dimen/_5sdp"
android:layout_marginBottom="@dimen/_5sdp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_total"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:gravity="start"
android:text="@string/total"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="32dp"
android:gravity="start"
android:text="125$"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="@dimen/_5sdp"
android:background="@android:color/black" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginStart="5dp"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginEnd="32dp"
android:gravity="center"
android:text="@string/order_tpass_message"
android:textColor="@android:color/black"
android:textSize="@dimen/_30sdp" />
</LinearLayout>
</ScrollView>
这是创建位图的代码
fun getBitmapFromView(view: View): Bitmap? {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
val bitmap = Bitmap.createBitmap(
view.measuredWidth, view.measuredHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
view.layout(0, 0, view.width, view.height)
view.draw(canvas)
return bitmap
}
我已将 recylcview 背景标记为红色以更好地了解发生了什么。
结果你可以看到这个
谁能告诉我回收视图比预期的要大的原因是什么。
我又测试了一件事。当我试图在我的屏幕上显示这个视图时,红色空间不一样。它的高度应该是可以的。
当我创建位图时,结果不好。