Search code examples
androidviewimageviewandroid-custom-view

Custom Image View Cut Left and Right Edges Semi Circle


Need help with developing a custom view. This is the kind of view I need to develop: enter image description here

Have tried a way, but it does not look like a card, its curve and card can be seen in the background. Need something transparent at the cut left and right edges.

Have referred https://github.com/mreram/TicketView, but this does not give a card like effect, need a view with cut edge at left and right in the middle and looks like a card. Any ideas on customizing an image view to look like this ?


Solution

  • I hope this will help you, I did create a class long before which extends FrameLayout which can do the work for you.

    Here is what the output will look like

    Result Image

    I achieved this by following code.

    MyRoundCornerFrame.java

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Path;
    import android.graphics.RectF;
    import android.graphics.Region;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.util.TypedValue;
    import android.view.View;
    import android.widget.FrameLayout;
    
    public class MyRoundCornerFrame extends FrameLayout {
        private final static float CORNER_RADIUS = 30.0f; //card radius chnage accordingly
        private float cornerRadius;
    
        public MyRoundCornerFrame(Context context) {
            super(context);
            init(context, null, 0);
        }
    
        public MyRoundCornerFrame(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs, 0);
        }
    
        public MyRoundCornerFrame(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context, attrs, defStyle);
        }
    
        private void init(Context context, AttributeSet attrs, int defStyle) {
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            int count = canvas.save();
    
            final Path path = new Path();
            path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
            canvas.clipPath(path, Region.Op.INTERSECT);
    
            canvas.clipPath(path);
            super.dispatchDraw(canvas);
            canvas.restoreToCount(count);
        }
    }
    

    My XML file looks like below

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:paddingTop="30dp">
    
        <your.package.name.MyRoundCornerFrame
            android:id="@+id/card_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="20dp">
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@drawable/food"
                android:padding="20dp" />
    
        </your.package.name.MyRoundCornerFrame>
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:background="@drawable/ticket_view"
            android:padding="10dp"
            tools:ignore="ContentDescription" />
    
    
    </FrameLayout>
    

    for displaying semi-circles at the center I created an XML which named ticket_view.xml layer-list which looks like below

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="5dp" />
            </shape>
        </item>
    
        <item
            android:width="50dp"
            android:height="50dp"
            android:gravity="start|center">
            <shape android:shape="oval">
                <solid android:color="@android:color/white" />
    
            </shape>
        </item>
    
        <item
            android:width="50dp"
            android:height="50dp"
            android:gravity="end|center">
            <shape android:shape="oval">
                <solid android:color="@android:color/white" />
    
            </shape>
        </item>
    
    
    </layer-list>
    

    A plus point for this is you don't need any library to create this.

    I hope this will help you somehow, happy coding