简体   繁体   English

如何在 android 中创建这样的布局?

[英]How to create layout like this in android?

I want to create one screen where I can click image of card as shown in below given image.我想创建一个屏幕,我可以在其中单击卡片图像,如下图所示。

在此处输入图像描述

I have created screen like this with code as given below and this code actually works but I'm just not sure how to add that card cutout layout on this.我已经使用下面给出的代码创建了这样的屏幕,并且此代码确实有效,但我只是不确定如何在此添加该卡片切口布局。

<RelativeLayout
            android:id="@+id/rlCamera"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">

            <androidx.camera.view.PreviewView
                android:id="@+id/viewFinder"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alpha="0.5"
                android:background="@color/black" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:layout_width="@dimen/_24sdp"
                    android:layout_height="@dimen/_24sdp"
                    android:layout_margin="@dimen/_15sdp">

                    <com.google.android.material.card.MaterialCardView
                        android:layout_width="@dimen/_34sdp"
                        android:layout_height="@dimen/_34sdp"
                        android:alpha="0.2"
                        app:cardBackgroundColor="@color/white"
                        app:cardCornerRadius="@dimen/_50sdp" />

                    <androidx.appcompat.widget.AppCompatImageView
                        android:layout_width="@dimen/_15sdp"
                        android:layout_height="@dimen/_15sdp"
                        android:layout_centerInParent="true"
                        android:src="@drawable/ic_close"
                        android:tint="@color/white" />

                </RelativeLayout>

                <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="@dimen/_25sdp"
                    android:text="License"
                    android:textColor="@color/white"
                    android:textSize="@dimen/_14sdp"
                    android:textStyle="bold" />

                <androidx.appcompat.widget.LinearLayoutCompat
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_120sdp"
                    android:gravity="center_horizontal"
                    android:orientation="vertical">

                    <androidx.camera.view.PreviewView
                        android:id="@+id/viewFinderCard"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/_150sdp"
                        android:layout_marginHorizontal="@dimen/_40sdp" />

                    <androidx.appcompat.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_40sdp"
                        android:text="Front of card"
                        android:textColor="@color/white"
                        android:textSize="@dimen/_14sdp"
                        android:textStyle="bold" />

                    <androidx.appcompat.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginHorizontal="@dimen/_70sdp"
                        android:layout_marginTop="@dimen/_10sdp"
                        android:text="All 4 corners should be aligned to card given above."
                        android:textAlignment="center"
                        android:textColor="@color/white"
                        android:textSize="@dimen/_12sdp" />

                </androidx.appcompat.widget.LinearLayoutCompat>

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/imgCapture"
                    android:layout_width="@dimen/_54sdp"
                    android:layout_height="@dimen/_54sdp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="@dimen/_40sdp"
                    android:src="@drawable/ic_capture" />

            </RelativeLayout>

        </RelativeLayout>

Result结果

在此处输入图像描述

But now I'm not sure how to add the transparent cutout for card image rectangle.但现在我不确定如何为卡片图像矩形添加透明切口。

Can anyone help me with this?谁能帮我这个?

I strongly recommend to use 9-patch bitmap for such cutout.我强烈建议使用9-patch bitmap 来实现这种切口。 if some ViewGroup have solid/semitransparent/image as a background then its hard to "cut out" some part of it from child side, you should override onDraw then and make some manual math and job on Canvas ... it should be at least drawable , but your sample isn't easy to be made programmatically (especially colooured corners).如果某些ViewGroup有实心/半透明/图像作为背景,那么很难从子方“切出”其中的某些部分,你应该覆盖onDraw然后在Canvas上进行一些手动数学和工作......它应该至少drawable ,但您的样本不容易以编程方式制作(尤其是彩色角落)。

would be way easier to set 9-patch as background for whole layout in place of currently set "@color/black" and alpha .将 9-patch 设置为整个布局的背景来代替当前设置的"@color/black"alpha会更容易。 be aware of content and stretch areas and set some maxWidth and maxHeight (or fixed) for frame with card preview.注意内容和拉伸区域,并为带有卡片预览的框架设置一些maxWidthmaxHeight (或固定)。 9-patch will wrap it nicely 9-patch 会很好地包裹它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM