微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 自定义视图,使用不同的子视图对角分割布局

如何将LinearLayout或RelativeLayout对角分割成两种不同的大小,每种大小都有不同的子视图.上半部分中的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,getWidth(),getHeight(),null,Canvas.ALL_SAVE_FLAG);
    super.dispatchDraw(canvas);

    paint.setXfermode(pdMode);
    path.reset();
    path.moveto(0,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>

顺便说一句.这个东西最近很受欢迎:)

原文地址:https://www.jb51.cc/android/313661.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐