Making an image circular with white circular border

后端 未结 10 2449
甜味超标
甜味超标 2021-02-19 19:32

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 wa

相关标签:
10条回答
  • 2021-02-19 20:19
    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;
    }
    
    0 讨论(0)
  • 2021-02-19 20:22

    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>
    
    0 讨论(0)
  • 2021-02-19 20:22

    First add below line to build.gradle file

    implementation 'de.hdodenhof:circleimageview:2.2.0'
    

    then add following XML code to xml file:

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

    0 讨论(0)
  • 2021-02-19 20:23

    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

    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"
    
    0 讨论(0)
提交回复
热议问题