android - 使用不同的 subview 对角拆分布局的自定义 View

标签 android android-layout android-viewpager android-linearlayout

我如何将 LinearLayoutRelativeLayout 对角线拆分成两个不同的大小,每个都有不同的 subview 。上半部分的示例 ViewPager 和下半部分的不同 LinearLayout

是这样的:

Irregular shaped View Pager

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

最佳答案

最简单的方法是只制作一个带有倾斜切割的背景图像。如果您希望拥有动态布局并且想要真正剪切小部件,请使用 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>

enter image description here

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

关于android - 使用不同的 subview 对角拆分布局的自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338840/

相关文章:

android - ActionBarSherlock + Tabbed Fragments + ViewPager inside a fragment

安卓工作室 : The application could not be installed: SHELL_UNRESPONSIVE

android - 在同一个类中访问变量

android - 如何在android中设置屏幕底部的标签?

Android 动态布局仅在 oncreate 方法完成后显示

android - 在线性布局中动态添加textview

Android拆分操作栏,顶部和底部都有操作项?

java - 有什么方法可以获取最新的聚合数据,而不会成为 Firebase 列表中的最后一个?

android - 如何禁用背后 View 点击事件Framelayout

android - Recyclerview 项目首次未显示