简体   繁体   中英

How to draw custom background for Fragment/CustomView using Android Canvas?

I am creating a custom fragment that will serve as a custom navigation drawer that slides up from the bottom. Problem is I need to create a curve for the top of the fragment's background. I need some help to get the curve by drawing the shape using the Canvas. Not sure if I should create and blend two shapes together to get my desired results or just create a custom background image.

Here is an image of a mock I created in Sketch: Curved Nav Drawer Mock

You can do it by wrapping your layout in cardview and set corner radius of cardview to 10dp. By this you can achieve curve in your drawer fragment.

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view_outer"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@android:color/transparent"
    card_view:cardCornerRadius="10dp"
    card_view:cardElevation="3dp" >

    <!-- Inner layout -->

</android.support.v7.widget.CardView>

So I solved the problem by taking a vector image of the shape that was created in Sketch by first making it a symbol in Sketch. I then exported the symbol as a vector(SVG) file.

To get the vector to configure properly I used SVG to Vector Converter which I then used to create an xml file that I pasted into my drawable folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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