简体   繁体   English

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

[英]How to draw angled rectangle as background for ViewPager android

I want to implement a ViewPager having angled rectangle as background as shown in the image. 我想实现一个ViewPager,它具有成角度的矩形作为背景,如图中所示。 I have tried to get it done by setting xml as background. 我试图通过将xml设置为背景来完成它。 But it only displays properly if we give image in that xml itself and if I try to manage it runtime then it is not taking that angled background. 但是,只有当我们在xml本身中提供图像并且如果我尝试管理它的运行时时,它才能正确显示,否则它不会使用该角度的背景。

This is the drawable I have attached to custom viewpager layout 这是我附加到自定义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>

AND this is the custom viewpager layout 这是自定义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 它应该是这样显示的

Can anyone please tell me how to do it? 谁能告诉我该怎么做?

This is how it should display 它应该是这样显示的

checked this link. 检查了此链接。

based on above link i guess as page swipes you want change background. 基于以上链接,我想当您要滑动页面时更改背景。

solution : 解决方案:

  1. use constraint_layout or frame_layout. 使用constraint_layout或frame_layout。
  2. add view pager as match_parent in height and width. 将视图传呼器的高度和宽度添加为match_parent。
  3. angle view use it and view on top of view_pager. 角度视图使用它并在view_pager顶部查看。

suggestion : use constraint layout. 建议:使用约束布局。

note : above solution will not change view_pager dimensions (dont change it since view is complex, if you change behaviour unpredictable). 注意:上述解决方案不会更改view_pager的尺寸(如果更改行为无法预测,则因为视图很复杂,因此请勿更改)。 Only you end up with place custom view on top of pager. 只有您最终将自定义视图放在寻呼机上。

If you need to learn how to create a diagonal cut you could have a look at this tutorial: https://medium.com/@adinugroho/create-diagonal-cut-view-in-android-5a376eca6a1c 如果您需要学习如何创建对角线切口,可以看一下本教程: https : //medium.com/@adinugroho/create-diagonal-cut-view-in-android-5a376eca6a1c

If you need a library to do it for you then have a look at this library: https://github.com/florent37/DiagonalLayout 如果您需要一个库来为您做这件事,那么看看这个库: https : //github.com/florent37/DiagonalLayout

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

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