use*_*910 180 xml android imageview
我想把我的任何图像ImageView变成带边框的圆形.
我搜索但找不到任何有用的信息(我试过的任何东西都不起作用).
我如何通过xml实现这一点:ImageView使用某个src 创建一个并使其带边框的圆形?
shr*_*hat 259
这是我设计的最简单的方法.试试这个.
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>
Run Code Online (Sandbox Code Playgroud)
如果您正在使用lollipop以上的Android版本
<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>
Run Code Online (Sandbox Code Playgroud)
Orh*_*but 208
您可以制作一个带有白色边框和透明内容的简单圆圈.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="10dp"
android:color="@android:color/white" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后创建一个可绘制的图层列表,并将其作为背景添加到您的图像视图中.
// res/drawable/img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/ic_launcher"/>
<item android:drawable="@drawable/circle"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
并将其作为您的imageview的背景.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img"/>
Run Code Online (Sandbox Code Playgroud)
你会有类似的东西.

San*_*iya 107
我希望这对你有所帮助.
1)使用第三方库
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)
注意:在项目中,打开your_app> Gradle Scripts> build.gradle(Module:app)并将以下实现语句添加到依赖项{}
implementation 'de.hdodenhof:circleimageview:3.0.0'
Run Code Online (Sandbox Code Playgroud)
2)没有第三方库
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
Run Code Online (Sandbox Code Playgroud)
RES /值/ attrs.xml
implementation 'com.mikhaellopez:circularimageview:3.2.0'
Run Code Online (Sandbox Code Playgroud)
布局
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 76
使用材料组件库只需使用ShapeableImageView.
类似的东西:
<com.google.android.material.imageview.ShapeableImageView
app:shapeAppearanceOverlay="@style/roundedImageViewRounded"
app:strokeColor="@color/....."
app:strokeWidth="1dp"
...
/>
Run Code Online (Sandbox Code Playgroud)
和:
<style name="roundedImageViewRounded">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
注意:它至少需要版本1.2.0.
使用jetpack compose, 1.0.x您可以clip使用以下命令应用修改器CircleShape:
Image(
painter = painterResource(R.drawable.xxxx),
contentDescription = "xxxx",
contentScale = ContentScale.Crop,
modifier = Modifier
.size(100.dp)
.clip(CircleShape)
.border(2.dp, Color.Blue, CircleShape)
)
Run Code Online (Sandbox Code Playgroud)
Jyo*_*ngh 30
如果您使用该src属性,上述方法似乎不起作用.我做的是将两个图像视图放在一个框架布局内,一个在另一个上面,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
只需将circular_crop.png放在您的可绘制文件夹中,该文件夹的形状为图像尺寸(在我的情况下为正方形),中间有白色背景和透明圆圈.如果您需要方形图像视图,可以使用此图像.
只需下载上面的图片.
Chi*_*ang 28
借助滑动库和RoundedBitmapDrawableFactory类,它很容易实现.您可能需要创建圆形占位符图像.
滑翔V4:
Glide.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView);
Run Code Online (Sandbox Code Playgroud)
Glide V3:
Glide.with(context)
.load(imgUrl)
.asBitmap()
.placeholder(R.drawable.placeholder)
.error(R.drawable.placeholder)
.into(new BitmapImageViewTarget(imgProfilePicture) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
Bitmap.createScaledBitmap(resource, 50, 50, false));
drawable.setCircular(true);
imgProfilePicture.setImageDrawable(drawable);
}
});
Run Code Online (Sandbox Code Playgroud)
Bug*_*ggy 19
以下是最简单的方法之一,使用以下代码:
依赖
dependencies {
...
compile 'de.hdodenhof:circleimageview:2.1.0' // use this or use the latest compile version. In case u get bug.
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
截图:
来源: Circular ImageView GitHub存储库
小智 13
您可以简单地使用 AndroidX ImageFilterView。
<androidx.constraintlayout.utils.widget.ImageFilterView
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginStart="@dimen/margin_medium"
android:layout_marginBottom="@dimen/margin_medium"
android:background="@color/white"
android:padding="@dimen/margin_small"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:roundPercent="1"
app:srcCompat="@drawable/ic_gallery" />
Run Code Online (Sandbox Code Playgroud)
您不需要任何第三方库。
您可以ShapeableImageView在材料中使用 。
implementation 'com.google.android.material:material:1.2.0'
Run Code Online (Sandbox Code Playgroud)
样式文件
<style name="ShapeAppearanceOverlay.App.CornerSize">
<item name="cornerSize">50%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在布局中
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_profile"
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.CornerSize"
/>
Run Code Online (Sandbox Code Playgroud)
你可以看到这个
https://developer.android.com/reference/com/google/android/material/imageview/ShapeableImageView
或这个
https://medium.com/android-beginners/shapeableimageview-material-components-for-android-cac6edac2c0d
您可以简单地使用 CardView 而无需任何外部库
<androidx.cardview.widget.CardView
android:id="@+id/roundCardView"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerHorizontal="true"
android:elevation="0dp"
app:cardCornerRadius="20dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/profile" />
</androidx.cardview.widget.CardView>
Run Code Online (Sandbox Code Playgroud)
小智 6
发布此答案以供将来参考。您可以使用ShapeableImageView可用的com.google.android.material:material.
<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" />
Run Code Online (Sandbox Code Playgroud)
imageViewRounded在 styles.xml 中添加样式
<style name="imageViewRounded">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">51%</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如果没有添加,您可以添加材料设计依赖项。
implementation 'com.google.android.material:material:1.4.0'
Run Code Online (Sandbox Code Playgroud)
设计看起来像
小智 5
这将达到目的:
矩形文件
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/transparent" />
<padding android:bottom="-14dp" android:left="-14dp" android:right="-14dp" android:top="-14dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="oval"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="15dp"
android:color="@color/verification_contact_background" />
</shape>
Run Code Online (Sandbox Code Playgroud)
profile_image.xml(图层列表)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/rectangle" />
<item android:drawable="@drawable/circle"/>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
您的布局
<ImageView
android:id="@+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/default_org"
android:src="@drawable/profile_image"/>
Run Code Online (Sandbox Code Playgroud)
最佳解决方案礼貌https://www.youtube.com/watch?v=0MHoNU7ytaw 卡片视图的宽度和高度决定了它包含的图像的大小设置如下:
implementation 'androidx.cardview:cardview:1.0.0'
<androidx.cardview.widget.CardView
android:layout_width="300dp"
android:layout_height="270dp"
android:layout_gravity="center"
app:cardCornerRadius="150dp"
app:cardBackgroundColor="@color/trans"
>
<ImageView
android:id="@+id/resultImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/congrats"
android:layout_gravity="center">
</ImageView>
</androidx.cardview.widget.CardView>```
Run Code Online (Sandbox Code Playgroud)
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
Run Code Online (Sandbox Code Playgroud)
XML
<ImageView
android:id="@+id/vinyl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription" />
Run Code Online (Sandbox Code Playgroud)
在代码中
Glide.with(this)
.load("https://images.pexels.com/photos/3828241/pexels-photo-3828241.jpeg")
.transform(CircleCrop())
.into(rootView.findViewById<ImageView>(R.id.vinyl))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
389014 次 |
| 最近记录: |