如何绘制有角度的矩形作为ViewPager android的背景



我想实现一个ViewPager,它的背景是倾斜的矩形,如图所示。我试图通过将xml设置为背景来完成这项工作。但只有当我们在xml中给出图像时,它才能正确显示,如果我试图在运行时管理它,那么它就不会采用倾斜的背景。

这是我附在自定义视图寻呼机布局上的绘图

<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>

这是自定义的视图寻呼机布局

<?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>

这就是它应该如何显示

有人能告诉我怎么做吗?

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

如果你需要一个图书馆来为你做这件事,那么看看这个图书馆:https://github.com/florent37/DiagonalLayout

这就是它应该如何显示

选中了此链接。

基于上面的链接,我想随着页面的滑动,你想改变背景。

解决方案:

  1. 使用constraint_layout或frame_layout
  2. 在高度和宽度上添加视图寻呼机作为match_parent
  3. angle-view使用它并在view_pager顶部查看

建议:使用约束布局。

注意:上面的解决方案不会改变viewpage的维度(不要改变它,因为视图很复杂,如果你改变行为是不可预测的(。只有您最终才能将自定义视图放置在寻呼机的顶部。

最新更新