我想让任何图像从我的ImageView是圆形的边界。
我搜索了一下,但没有找到任何有用的信息(我尝试的任何方法都不管用)。
如何通过XML实现这一点: 创建一个ImageView与某些src,并使它与边界圆形?
我想让任何图像从我的ImageView是圆形的边界。
我搜索了一下,但没有找到任何有用的信息(我尝试的任何方法都不管用)。
如何通过XML实现这一点: 创建一个ImageView与某些src,并使它与边界圆形?
当前回答
把这个答案贴出来供将来参考。你可以在com.google.android.material:material中使用ShapeableImageView。
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/img_launcher_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:elevation="5dp"
android:maxWidth="50dp"
android:maxHeight="50dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
app:shapeAppearance="?attr/actionButtonStyle"
app:shapeAppearanceOverlay="@style/imageViewRounded"
app:strokeColor="@android:color/white" />
在你的styles.xml中添加imageviewraded样式
<style name="imageViewRounded">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">51%</item>
</style>
如果没有添加,可以添加材料设计依赖项。
implementation 'com.google.android.material:material:1.4.0'
设计是这样的
其他回答
把这个答案贴出来供将来参考。你可以在com.google.android.material:material中使用ShapeableImageView。
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/img_launcher_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:elevation="5dp"
android:maxWidth="50dp"
android:maxHeight="50dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
app:shapeAppearance="?attr/actionButtonStyle"
app:shapeAppearanceOverlay="@style/imageViewRounded"
app:strokeColor="@android:color/white" />
在你的styles.xml中添加imageviewraded样式
<style name="imageViewRounded">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">51%</item>
</style>
如果没有添加,可以添加材料设计依赖项。
implementation 'com.google.android.material:material:1.4.0'
设计是这样的
只需使用这段简单的代码: 首先添加依赖项:
implementation 'de.hdodenhof:circleimageview:2.2.0'
然后在XML布局中添加以下代码
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/Imgshaligram"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/shaligram"
app:civ_border_color="#d1b1b1"
android:foregroundGravity="center"/>
下面是最简单的方法之一,使用以下代码:
依赖关系
dependencies {
...
compile 'de.hdodenhof:circleimageview:2.1.0' // use this or use the latest compile version. In case u get bug.
}
XML代码
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp" // here u can adjust the width
android:layout_height="96dp" // here u can adjust the height
android:src="@drawable/profile" // here u can change the image
app:civ_border_width="2dp" // here u can adjust the border of the circle.
app:civ_border_color="#FF000000"/> // here u can adjust the border color
截图:
来源:Circular ImageView GitHub Repository
if you want to set edit icon on to circle imageview than put this below code.
<FrameLayout
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_100sdp"
android:layout_gravity="center"
android:layout_marginTop="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profilePic"
android:layout_width="@dimen/_100sdp"
android:layout_height="@dimen/_100sdp"
android:layout_gravity="bottom|center_horizontal"
android:src="@drawable/ic_upload" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_camera"
android:layout_width="@dimen/_30sdp"
android:layout_height="@dimen/_30sdp"
android:layout_gravity="top|right"
android:src="@drawable/edit"/>
</FrameLayout>
如果你在应用中使用材质设计,那么就使用这个
<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>