How to make a view with rounded corners?

前端 未结 20 1012
遇见更好的自我
遇见更好的自我 2020-11-27 11:08

I am trying to make a view in android with rounded edges. The solution I found so far is to define a shape with rounded corners and use it as the background of that view.

相关标签:
20条回答
  • 2020-11-27 11:46

    In Android L you will be able to just use View.setClipToOutline to get that effect. In previous versions there is no way to just clip the contents of a random ViewGroup in a certain shape.

    You will have to think of something that would give you a similar effect:

    • If you only need rounded corners in the ImageView, you can use a shader to 'paint' the image over the shape you are using as background. Take a look at this library for an example.

    • If you really need every children to be clipped, maybe you can another view over your layout? One with a background of whatever color you are using, and a round 'hole' in the middle? You could actually create a custom ViewGroup that draws that shape over every children overriding the onDraw method.

    0 讨论(0)
  • 2020-11-27 11:46

    Difference from Jaap van Hengstum's answer:

    1. Use BitmapShader instead of mask bitmap.
    2. Create bitmap only once.
    public class RoundedFrameLayout extends FrameLayout {
        private Bitmap mOffscreenBitmap;
        private Canvas mOffscreenCanvas;
        private BitmapShader mBitmapShader;
        private Paint mPaint;
        private RectF mRectF;
    
        public RoundedFrameLayout(Context context) {
            super(context);
            init();
        }
    
        public RoundedFrameLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public RoundedFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            setWillNotDraw(false);
        }
    
        @Override
        public void draw(Canvas canvas) {
            if (mOffscreenBitmap == null) {
                mOffscreenBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
                mOffscreenCanvas = new Canvas(mOffscreenBitmap);
                mBitmapShader = new BitmapShader(mOffscreenBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                mPaint.setShader(mBitmapShader);
                mRectF = new RectF(0f, 0f, canvas.getWidth(), canvas.getHeight());
            }
            super.draw(mOffscreenCanvas);
    
            canvas.drawRoundRect(mRectF, 8, 8, mPaint);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 11:47

    Or you can use a android.support.v7.widget.CardView like so:

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardCornerRadius="4dp">
    
        <!--YOUR CONTENT-->
    </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-11-27 11:47

    Create a xml file under your drawable folder with following code. (The name of the file I created is rounded_corner.xml)

    rounded_corner.xml

        <?xml version="1.0" encoding="utf-8"?>
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
    
            <!-- view background color -->
            <solid
                android:color="#a9c5ac" >
            </solid>
    
            <!-- view border color and width -->
            <stroke
                android:width="3dp"
                android:color="#1c1b20" >
            </stroke>
    
            <!-- If you want to add some padding -->
            <padding
                android:left="4dp"
                android:top="4dp"
                android:right="4dp"
                android:bottom="4dp"    >
            </padding>
    
            <!-- Here is the corner radius -->
            <corners
                android:radius="10dp"   >
            </corners>
        </shape>
    

    And keep this drawable as background for the view to which you want to keep rounded corner border. Let’s keep it for a LinearLayout

        <LinearLayout android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/rounded_corner"
                android:layout_centerInParent="true">
    
                <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Hi, This layout has rounded corner borders ..."
                    android:gravity="center"
                    android:padding="5dp"/>
    
        </LinearLayout>
    
    0 讨论(0)
  • 2020-11-27 11:48

    The CardView worked for me in API 27 in Android Studio 3.0.1. The colorPrimary was referenced in the res/values/colors.xml file and is just an example. For the layout_width of 0dp it will stretch to the width of the parent. You'll have to configure the constraints and width/height to your needs.

    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        app:cardBackgroundColor="@color/colorPrimary">
    
        <!-- put your content here -->
    
    </android.support.v7.widget.CardView>
    
    0 讨论(0)
  • 2020-11-27 11:48

    Use shape in xml with rectangle.set the property of bottom or upper radius as want.then apply that xml as background to ur view....or...use gradients to do it from code.

    0 讨论(0)
提交回复
热议问题