Android 制作带有白色圆形边框的圆形图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21871833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:13:15  来源:igfitidea点击:

Making an image circular with white circular border

androidlayoutimageviewgeometrydrawing

提问by Gopal Gopi

How to make an image circular and give it white circular border? Is it necessary to use two image views – one for the image and other for the white border? Is there any other way to do this?

如何使图像圆形并为其提供白色圆形边框?是否有必要使用两个图像视图——一个用于图像,另一个用于白色边框?有没有其他方法可以做到这一点?

回答by Gopal Gopi

Try this...

尝试这个...

public static Bitmap getCircularBitmapWithWhiteBorder(Bitmap bitmap,
        int borderWidth) {
    if (bitmap == null || bitmap.isRecycled()) {
        return null;
    }

    final int width = bitmap.getWidth() + borderWidth;
    final int height = bitmap.getHeight() + borderWidth;

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    Canvas canvas = new Canvas(canvasBitmap);
    float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setShader(null);
    paint.setStyle(Paint.Style.STROKE);
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(borderWidth);
    canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, paint);
    return canvasBitmap;
}

回答by Md Nakibul Hassan

First add below line to build.gradlefile

首先将以下行添加到build.gradle文件

implementation 'de.hdodenhof:circleimageview:2.2.0'

then add following XMLcode to xmlfile:

然后将以下XML代码添加到xml文件中:

<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"/>

enter image description here

在此处输入图片说明

回答by SweetWisher ツ

First get Circulat image with your code. Then apply this xml :

首先使用您的代码获取 Circulat 图像。然后应用这个 xml :

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
    <gradient android:startColor="#333440" android:endColor="#333440"
        android:angle="270"/>
</shape>

Then add a relative layout and add an imageview to it.Arrange it to the center of relative layout.And set this circle shape as Imageview's background.Then place your circular imageview above previously added imageview.Arrange it also to center.By changing your circular imageview margin you will get the desired border effect. Hope this will help you..

然后添加一个相对布局并向其添加一个imageview。将其排列到相对布局的中心。并将此圆形形状设置为Imageview的背景。然后将圆形imageview放在先前添加的imageview之上。将其也排列到中心。通过更改圆形imageview margin 你会得到想要的边框效果。希望能帮到你..

回答by Marialena

This may not be the best way and you may not be able to change a lot of properties, but it is surely the easiest way. I just used thislibrary and I made a circular imageview that has also a border.

这可能不是最好的方法,您可能无法更改很多属性,但它肯定是最简单的方法。我刚刚使用了这个库,我制作了一个圆形图像视图,它也有一个边框。



So, this is my solution:

所以,这是我的解决方案:

First, I put this in my build.grandle:

首先,我把它放在我的build.grandle

`compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'`

Second, I put this in my .xmllayout file:

其次,我把它放在我的.xml布局文件中:

 <com.github.siyamed.shapeimageview.CircularImageView
                    android:layout_width="150dp"
                    android:layout_gravity="center_horizontal"
                    android:layout_height="150dp"
                    android:id="@+id/photo"
                    app:siBorderWidth="5dp"
                    app:siBorderColor="#ffffff"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true" />

In my .javafile, this is the way I can take or set the image to the CircularImageView:

在我的.java文件中,这是我可以采取或设置图像的方式CircularImageView

final com.github.siyamed.shapeimageview.CircularImageView photo = (com.github.siyamed.shapeimageview.CircularImageView) convertView.findViewById(R.id.photo);

photo.setBackgroundResource(R.drawable.female);


That's all I've done to do the an image circular with white border.

这就是我为制作带有白色边框的图像圆形所做的全部工作。

回答by InnocentKiller

No it is not necessary that you have to use two image views one for the image and other for the white border. You can create one new XML file like below

不,您不必使用两个图像视图,一个用于图像,另一个用于白色边框。您可以创建一个新的 XML 文件,如下所示

border.xml

边框文件

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="5dp" android:color="#000000" />
<padding android:left="5dp" android:top="5dp" android:right="5dp"
    android:bottom="5dp" />
</shape>

and then set it to your image-view. Like add below line to your image-view.

然后将其设置为您的图像视图。就像将下面的行添加到您的图像视图中一样。

android:background="@drawable/border"

回答by Praveen Sharma

Here is a nice tutorialfor it.

这是一个很好的教程

in this tutorial they use a Method:-

在本教程中,他们使用一种方法:-

/* * Making image in circular shape */

/* * 制作圆形图像 */

public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
  // TODO Auto-generated method stub
  int targetWidth = 50;
  int targetHeight = 50;
  Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, 
                            targetHeight,Bitmap.Config.ARGB_8888);

                Canvas canvas = new Canvas(targetBitmap);
  Path path = new Path();
  path.addCircle(((float) targetWidth - 1) / 2,
  ((float) targetHeight - 1) / 2,
  (Math.min(((float) targetWidth), 
                ((float) targetHeight)) / 2),
          Path.Direction.CCW);

                canvas.clipPath(path);
  Bitmap sourceBitmap = scaleBitmapImage;
  canvas.drawBitmap(sourceBitmap, 
                                new Rect(0, 0, sourceBitmap.getWidth(),
    sourceBitmap.getHeight()), 
                                new Rect(0, 0, targetWidth,
    targetHeight), null);
  return targetBitmap;
 }

For providing border around your imageView :

为了在你的 imageView 周围提供边框:

Add this xml inside your drawable folder :

将此 xml 添加到 drawable 文件夹中:

=>rounded.xml

=>rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="@android:color/white" />

    <stroke
        android:width="3dip"
        android:color="#FF0000" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />

</shape>

Hope , this will helps

希望,这会有所帮助

回答by Syed Raza Mehdi

circular image view with rounded white border

带有圆形白色边框的圆形图像视图

By using this linki have successfully made it, using a FrameLayoutand twoRoundedImageView. The logic behind what i did is one is a wrapper view and one is the view with the profile image. Here is my code

通过使用此链接,我成功地使用了 aFrameLayouttwoRoundedImageView。我所做的背后的逻辑是一个包装视图,一个是带有个人资料图像的视图。这是我的代码

XML code:

XML 代码:

    <FrameLayout
    android:id="@+id/llImageProfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="40dp"
    android:foregroundGravity="center">


    <com.pepperpk.frt.mallpk.custom.RoundedImageView
        android:id="@+id/circleViewOverlay"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center" />

    <com.pepperpk.frt.mallpk.custom.RoundedImageView
        android:id="@+id/circleView"
        android:layout_width="95dp"
        android:layout_height="95dp"
        android:layout_gravity="center" />
</FrameLayout>

JAVA code :

爪哇代码:

profileWrapper.setImageResource(R.drawable.white_background);
profile.setImageResource(R.drawable.profile);

hope it helps, if you have any confusion please comment below.

希望能帮到你,有什么不明白的请在下方留言。

回答by Max Droid

I tried keeping ImageView inside CardView and adjusted radius of the CardView accordingly.

我尝试将 ImageView 保留在 CardView 内并相应地调整 CardView 的半径。

NetworkImageView is the one from Volley Library. Same Should work with ImageView as well.

NetworkImageView 是来自 Volley Library 的那个。同样也应该与 ImageView 一起使用。

<android.support.v7.widget.CardView
            android:layout_width="105dp"
            android:layout_height="105dp"
            android:layout_margin="5dp"
            android:elevation="0dp"
            android:id="@+id/card_image_view"
            app:cardCornerRadius="53dp"
            android:innerRadius="0dp"
            android:background="@color/reco_widget_search_background"
            android:shape="ring"
            android:thicknessRatio="1">

            <NetworkImageView
                android:id="@+id/circle_networkImageViewProduct"
                android:layout_width="105dp"
                android:layout_height="105dp"
                android:backgroundTint="@color/white"
                android:tint="@color/white"
                android:scaleType="fitXY"
                android:layout_gravity="center"
                android:visibility="gone"
                />
        </android.support.v7.widget.CardView>

回答by Palak

Try This
    public static Bitmap createRoundImage(Bitmap loadedImage) {
    System.out.println("loadbitmap" + loadedImage);
    loadedImage = Bitmap.createScaledBitmap(loadedImage, 100, 100, true);
    Bitmap circleBitmap = Bitmap.createBitmap(loadedImage.getWidth(),
            loadedImage.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(loadedImage,
            Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(loadedImage.getWidth() / 2, loadedImage.getHeight() / 2,
            loadedImage.getWidth() / 2, paint);
    return circleBitmap;
}

回答by user3258691

Try this:

尝试这个:

<html>
<head>
<style type="text/css">
img {
border: 30px solid #FFFFFF;
border-radius: 130px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 10px;
}
body {
background: #000;
}
</style>
<meta charset="utf-8">
<title>try</title>
</head>
<body>
<img src="http://icons.iconarchive.com/icons/danleech/simple/1024/google-icon.png" width="48" height="48" alt=""/>
</body>
</html>