ImageView in circular through xml

前端 未结 27 1003
死守一世寂寞
死守一世寂寞 2020-11-22 10:35

I\'d Like to make any image from my ImageView to be circular with a border.

I searched but couldn\'t find any useful information (anything that I tried

相关标签:
27条回答
  • 2020-11-22 11:03

    This is the simplest way that I designed. Try this.

    dependencies: compile 'com.android.support:appcompat-v7:23.1.1'
                  compile 'com.android.support:design:23.1.1'
                  compile 'com.android.support:cardview-v7:23.1.1'
    
    <android.support.v7.widget.CardView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:elevation="12dp"
        android:id="@+id/view2"
       app:cardCornerRadius="40dp"
        android:layout_centerHorizontal="true"
        android:innerRadius="0dp"
        android:shape="ring"
        android:thicknessRatio="1.9">
        <ImageView
            android:layout_height="80dp"
            android:layout_width="match_parent"
            android:id="@+id/imageView1"
            android:src="@drawable/YOUR_IMAGE"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true">
        </ImageView>
     </android.support.v7.widget.CardView>
    

    If you are working on android versions above lollipop

    <android.support.v7.widget.CardView
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:elevation="12dp"
    android:id="@+id/view2"
    app:cardCornerRadius="40dp"
    android:layout_centerHorizontal="true">
    <ImageView
        android:layout_height="80dp"
        android:layout_width="match_parent"
        android:id="@+id/imageView1"
        android:src="@drawable/YOUR_IMAGE"
        android:scaleType="centerCrop"/>
      </android.support.v7.widget.CardView>
    

    Adding Border to round ImageView - LATEST VERSION

    Wrap it with another CardView slightly bigger than the inner one and set its background colour to add a border to your round image. You can increase the size of the outer CardView to increase the thickness of the border.

    <androidx.cardview.widget.CardView
      android:layout_width="155dp"
      android:layout_height="155dp"
      app:cardCornerRadius="250dp"
      app:cardBackgroundColor="@color/white">
    
        <androidx.cardview.widget.CardView
          android:layout_width="150dp"
          android:layout_height="150dp"
          app:cardCornerRadius="250dp"
          android:layout_gravity="center">
    
            <ImageView
              android:layout_width="150dp"
              android:layout_height="150dp"
              android:src="@drawable/default_user"
              android:scaleType="centerCrop"/>
    
       </androidx.cardview.widget.CardView>
    
     </androidx.cardview.widget.CardView>
    
    0 讨论(0)
  • 2020-11-22 11:05

    The above methods don't seem to work if you're using the src attribute. What I did is to put two image views inside a frame layout one above another like this:

    <FrameLayout android:id="@+id/frame"
                 android:layout_width="40dp"
                 android:layout_height="40dp">
    
        <ImageView android:id="@+id/pic"
                   android:layout_width="40dp"
                   android:layout_height="40dp"
                   android:src="@drawable/my_picture" />
    
        <ImageView android:id="@+id/circle_crop"
                   android:layout_width="40dp"
                   android:layout_height="40dp"
                   android:src="@drawable/circle_crop" />
    
    </FrameLayout>
    

    Simply put a circular_crop.png in your drawable folder which is in the shape of your image dimensions (a square in my case) with a white background and a transparent circle in the center. You can use this image if you have want a square imageview.

    Just download the picture above.

    0 讨论(0)
  • 2020-11-22 11:07

    If you use Material Design in your app then use this

    <com.google.android.material.card.MaterialCardView
                android:layout_width="75dp"
                android:layout_height="75dp"
                app:cardCornerRadius="50dp"
                app:strokeWidth="1dp"
                app:strokeColor="@color/black">
                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/circular_image"
                    android:scaleType="fitCenter"
                    android:src="@drawable/your_img" />
            </com.google.android.material.card.MaterialCardView>
    
    0 讨论(0)
提交回复
热议问题