使用不同的子视图对角拆分布局的自定义视图

Custom view that splits layout diagonally with different child views

我怎样才能将 LinearLayoutRelativeLayout 对角线分成两个不同的大小,每个都有不同的子视图。示例 ViewPager 在上半部分和不同的 LinearLayout 在下半部分。

像这样:

我怎样才能做到这一点?请帮忙

最简单的方法是只用那个倾斜的切口制作背景图像。如果你希望有一个动态布局并且你想真正地剪切小部件,请使用 Canvas.saveLayer/restore。像这样:

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Xfermode pdMode = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
private Path path = new Path();

protected void dispatchDraw(Canvas canvas) {
    int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
    super.dispatchDraw(canvas);

    paint.setXfermode(pdMode);
    path.reset();
    path.moveTo(0, getHeight());
    path.lineTo(getWidth(), getHeight());
    path.lineTo(getWidth(), getHeight() - TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics()));
    path.close();
    canvas.drawPath(path, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
}

要点:https://gist.github.com/ZieIony/8480b2d335c1aeb51167

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">

    <com.example.marcin.splitlayout.CutLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/mazda" />


    </com.example.marcin.splitlayout.CutLayout>

    <TextView
        android:layout_margin="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Mazda 3" />

</LinearLayout>

顺便说一句。这个东西最近很流行:)

您也可以尝试叠加底部背景并旋转它。也许您还必须在 x 方向上缩放它以避免在侧面剪裁。

我想我回答这个问题有点晚了,但是你应该看到 this amazing tutorial。 它很简单,没有任何外部库。 输出将类似于:

您需要添加layer-list

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary"/>
    <item>
        <bitmap android:src="@drawable/bebe" android:gravity="center" android:alpha="0.1"/>
    </item>

    <item android:top="300dp"
        android:bottom="-300dp"
        android:left="0dp"
        android:right="-300dp">
        <rotate
            android:fromDegrees="-10"
            android:pivotX="0%"
            android:pivotY="100%">
            <shape
                android:shape="rectangle">
                <solid
                    android:color="?android:colorBackground"/>
            </shape>
        </rotate>
    </item>
</layer-list>

现在您只需将这个可绘制对象添加到您的布局中。

布局如下:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/windowBackground"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/windowBackground"
            android:fitsSystemWindows="true">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorPrimary"
                app:expandedTitleMarginEnd="64dp"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleTextAppearance="@android:color/transparent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_collapseMode="parallax">

                    <RelativeLayout
                        android:id="@+id/background"
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:background="@drawable/background_profile_header"></RelativeLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/background"
                        android:gravity="center_vertical"
                        android:layout_marginStart="@dimen/activity_margin_16"
                        android:layout_marginEnd="@dimen/activity_margin_16"
                        android:layout_marginTop="-100dp">
                        <FrameLayout
                            android:layout_width="150dp"
                            android:layout_height="150dp">
                            <com.mikhaellopez.circularimageview.CircularImageView
                            android:id="@+id/circularProfilePic"
                            android:layout_width="150dp"
                            android:layout_height="150dp"
                            android:src="@drawable/profile"
                            app:civ_border_color="@color/semi_transparent"
                            app:civ_border_width="5dp" />

                            <ImageButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                app:fabSize="mini"
                                android:tint="@color/white"
                                android:backgroundTint="@color/red"
                                android:background="@color/red"
                                android:src="@android:drawable/ic_menu_camera"
                                android:padding="@dimen/activity_margin_5"
                                android:layout_gravity="right"
                                />
                        </FrameLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="bottom"
                            android:layout_marginBottom="@dimen/activity_margin_16"
                            android:layout_marginStart="12dp"
                            android:orientation="vertical"
                            android:layout_marginLeft="12dp">

                            <TextView
                                android:id="@+id/userName"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:maxLines="1"
                                android:fontFamily="sans-serif-light"
                                android:layout_marginLeft="@dimen/activity_margin_5"
                                android:text="Anuj Sharma"
                                android:textColor="@color/color_Primary_text"
                                android:textSize="30sp" />

                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="@dimen/activity_margin_10"
                                android:fontFamily="sans-serif-light"
                                android:text="musician, singer, songwriter"
                                android:textSize="14sp" />
                        </LinearLayout>
                    </LinearLayout>
                </RelativeLayout>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"></android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>


    </android.support.design.widget.CoordinatorLayout>

必须检查 this link 以便更好地理解。