如何绘制成角度的矩形作为ViewPager Android的背景

时间:2018-12-08 05:12:51

标签: java android xml

我想实现一个ViewPager,它具有成角度的矩形作为背景,如图中所示。我试图通过将xml设置为背景来完成它。但是,只有当我们在该xml本身中提供图像并且如果我尝试管理它的运行时时,它才能正确显示,那么它就不会使用该角度背景。

这是我附加到自定义Viewpager布局的可绘制对象

<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="@color/white"/>
    </shape>
</rotate>

这是自定义viewpager布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/rl_image"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="-60dp">

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/fashion1"
            app:civ_border_color="#FFF"
            app:civ_border_width="1dp" />

    </RelativeLayout>
</RelativeLayout>

This is how it should display

有人可以告诉我怎么做吗?

2 个答案:

答案 0 :(得分:0)

This is how it should display

检查了此链接。

基于上面的链接,我想当您要滑动页面来更改背景时。

解决方案:

  1. 使用constraint_layout或frame_layout。
  2. 将视图分页器的高度和宽度添加为match_parent。
  3. angle view使用它并在view_pager顶部查看。

建议:使用约束布局。

注意:以上解决方案不会更改view_pager的尺寸(如果更改行为不可预测,则因为视图很复杂,因此请勿更改)。只有最后,您才能将自定义视图放置在寻呼机顶部。

答案 1 :(得分:0)

如果您需要学习如何创建对角线切口,可以看一下本教程: https://medium.com/@adinugroho/create-diagonal-cut-view-in-android-5a376eca6a1c

如果您需要一个库来帮忙,那么请看一下这个库: https://github.com/florent37/DiagonalLayout