簡體   English   中英

如何繪制成角度的矩形作為ViewPager Android的背景

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

我想實現一個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>

它應該是這樣顯示的

誰能告訴我該怎么做?

它應該是這樣顯示的

檢查了此鏈接。

基於以上鏈接,我想當您要滑動頁面時更改背景。

解決方案:

  1. 使用constraint_layout或frame_layout。
  2. 將視圖傳呼器的高度和寬度添加為match_parent。
  3. 角度視圖使用它並在view_pager頂部查看。

建議:使用約束布局。

注意:上述解決方案不會更改view_pager的尺寸(如果更改行為無法預測,則因為視圖很復雜,因此請勿更改)。 只有您最終將自定義視圖放在尋呼機上。

如果您需要學習如何創建對角線切口,可以看一下本教程: https : //medium.com/@adinugroho/create-diagonal-cut-view-in-android-5a376eca6a1c

如果您需要一個庫來為您做這件事,那么看看這個庫: https : //github.com/florent37/DiagonalLayout

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM