Android ImageView 通过 xml 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22105775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImageView in circular through xml
提问by user3050910
I'd Like to make any image from my ImageView
to be circular with a border.
我想让我的任何图像ImageView
成为带边框的圆形。
I searched but couldn't find any useful information (anything that I tried didn't work).
我搜索但找不到任何有用的信息(我尝试过的任何方法都不起作用)。
How can I achieve this through xml:
Create an ImageView
with certain src and make it circular with a border?
我怎样才能通过 xml 实现这一点:ImageView
用特定的 src创建一个并使其带有边框的圆形?
回答by shreedhar bhat
This is the simplestway 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 版本
<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
为圆形 ImageView 添加边框 -最新版本
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.
用另一个比内部稍大的 CardView 包裹它,并设置它的背景颜色为圆形图像添加边框。您可以增加外部 CardView 的大小以增加边框的厚度。
<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>
回答by Orhan Obut
You can make a simple circle with white border and transparent content with shape.
您可以制作一个带有白色边框和带有形状的透明内容的简单圆圈。
// 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>
Then make a layerlist drawable and put it as background to your imageview.
然后制作一个可绘制的图层列表,并将其作为图像视图的背景。
// 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/circle"/>
<item android:drawable="@drawable/ic_launcher"/>
</layer-list>
and put it as background to your imageview.
并将其作为图像视图的背景。
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img"/>
You'll have something like that.
你会有这样的事情。
回答by Sanjay Mangaroliya
I hope this will help you.
我希望这能帮到您。
1) CircleImageView
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"/>
Don't forget implementation: Gradle Scripts > build.gradle (Module: app) > dependencies
不要忘记实现:Gradle Scripts > build.gradle (Module: app) > dependencies
implementation 'de.hdodenhof:circleimageview:3.1.0'
For complete description please check here : The Source here.
有关完整说明,请在此处查看:此处的来源。
2) CircularImageView
2) 圆形图像视图
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:civ_border_color="#3f51b5"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#3f51b5"/>
Don't forget implementation: Gradle Scripts > build.gradle (Module: app) > dependencies
不要忘记实现:Gradle Scripts > build.gradle (Module: app) > dependencies
implementation 'com.mikhaellopez:circularimageview:4.2.0'
For complete description please check here : The Source here.
有关完整说明,请在此处查看:此处的来源。
回答by Chitrang
With the help of glidelibrary and RoundedBitmapDrawableFactoryclass it's easy to achieve. You may need to create circular placeholder image.
借助glide库和RoundedBitmapDrawableFactory类很容易实现。您可能需要创建圆形占位符图像。
Glide V4:
滑翔V4:
Glide.with(context).load(url).apply(RequestOptions.circleCropTransform()).into(imageView);
Glide V3:
滑翔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);
}
});
For Picasso RoundedTransformation, this is a really great solution which gives an additional option of rounding image at either top or bottom edge.
对于 Picasso RoundedTransformation,这是一个非常好的解决方案,它提供了在顶部或底部边缘舍入图像的附加选项。
回答by Jyotman Singh
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:
如果您使用该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>
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.
只需将一个 circle_crop.png 放在您的可绘制文件夹中,该文件夹的形状为您的图像尺寸(在我的情况下为正方形),具有白色背景和中心的透明圆圈。如果您想要方形图像视图,则可以使用此图像。
Just download the picture above.
只需下载上面的图片。
回答by Bugs Buggy
The following is one of the simplest ways to do it, use the following code:
以下是最简单的方法之一,使用以下代码:
Dependencies
依赖关系
dependencies {
...
compile 'de.hdodenhof:circleimageview:2.1.0' // use this or use the latest compile version. In case u get bug.
}
XML Code
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
Screenshot:
截屏:
Source:Circular ImageView GitHub Repository
回答by Nidhi
This will do the trick:
这将解决问题:
rectangle.xml
矩形文件
<?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>
circle.xml
圈子.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>
profile_image.xml ( The layerlist )
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>
Your layout
你的布局
<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"/>
回答by Subramanya Sheshadri
I use shape = "oval" instead of the "ring" below. It has worked for me. To keep the image within bounds, I use <padding>
and set <adjustViewBounds>
to true in my <ImageView>
. I have tried with images of size between 50 x 50 px upto 200x200 px .
我使用 shape = "oval" 而不是下面的 "ring"。它对我有用。为了使范围内的图像,我使用<padding>
并设置<adjustViewBounds>
在我为true <ImageView>
。我尝试过大小在 50 x 50 px 到 200x200 px 之间的图像。
回答by Abdurahman Popal
You can simply use CardView without any external Library
您可以简单地使用 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>
回答by ekar
@Jyotman Singh, answer is very good (for solid backgrounds), so I would like to enhance it by sharing vector drawable that can be re-colored for your needs, also it is convenient since vector one-piece shape is well scalable.
@Jyotman Singh,答案非常好(对于纯色背景),所以我想通过共享可以根据您的需要重新着色的矢量可绘制对象来增强它,这也很方便,因为矢量整体形状具有很好的可扩展性。
This is the rectangle-circle shape (@drawable/shape_round_profile_pic):
这是矩形圆形(@drawable/shape_round_profile_pic):
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="284"
android:viewportHeight="284"
android:width="284dp"
android:height="284dp">
<path
android:pathData="M0 142L0 0l142 0 142 0 0 142 0 142 -142 0 -142 0zm165 137.34231c26.06742 -4.1212 52.67405 -17.543 72.66855 -36.65787 11.82805 -11.30768 20.55487 -22.85153 27.7633 -36.72531C290.23789 158.21592 285.62874 101.14121 253.48951 58.078079 217.58149 9.9651706 154.68849 -10.125717 98.348685 8.5190299 48.695824 24.95084 12.527764 67.047123 3.437787 118.98655 1.4806194 130.16966 1.511302 152.96723 3.4990422 164.5 12.168375 214.79902 47.646316 256.70775 96 273.76783c21.72002 7.66322 44.26673 9.48476 69 5.57448z"
android:fillColor="#ffffff" /> // you can change frame color
</vector>
Usage is the same:
用法是一样的:
<FrameLayout
android:layout_width="70dp"
android:layout_height="70dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/YOUR_PICTURE" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_round_profile_pic"/>
</FrameLayout>