Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular Image View in Android without using any external library

Is there any way to make a circular image view in Android without using any external libraries? Something in android design libraries? And I should be able to set the image at run time using code.

like image 992
Imal Avatar asked Sep 08 '17 09:09

Imal


3 Answers

You can put your ImageView inside CardView and set its corner radius. Simple example

<android.support.v7.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        card_view:cardCornerRadius="25dp"
        card_view:cardPreventCornerOverlap="false">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"/>
    </android.support.v7.widget.CardView>

Note that the corner radius should be twice shorter than image size.

like image 164
FuriousSpider Avatar answered Oct 05 '22 21:10

FuriousSpider


To have a circular ImageView with new Material components of Android use ShapeableImageView of package com.google.android.material.imageview.ShapeableImageView.

Property to apply is shapeAppearanceOverlay

Example:

<com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/ivUserPic"
            android:layout_width="@dimen/_75sdp"
            android:layout_height="@dimen/_75sdp"
            android:adjustViewBounds="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/labelTitle"
            app:shapeAppearanceOverlay="@style/ShapeAppearance.Image.PILL" />

style:

  <style name="ShapeAppearance.Image.PILL" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

<style name="ShapeAppearance.Image.Top.PILL" parent="">
    <item name="cornerSizeTopLeft">6dp</item>
    <item name="cornerFamilyTopLeft">rounded</item>
    <item name="cornerSizeTopRight">6dp</item>
    <item name="cornerFamilyTopRight">rounded</item>
</style>
like image 39
Dipali Shah Avatar answered Oct 05 '22 23:10

Dipali Shah


UPDATE

Now we can create circular imageView for Android without using any third-party library or custom ImageView

Use ShapeableImageView

Check out this to know how to use ShapeableImageView

OLD ANSWER

you can try this it is working fine in my device

create one RoundedImageView class like this

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundedImageView extends ImageView
{

    public RoundedImageView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {

        Drawable drawable = getDrawable();

        if (drawable == null)
        {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0)
        {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);

    }

    public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius)
    {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
                finalBitmap.getHeight() / 2 + 0.7f,
                finalBitmap.getWidth() / 2 + 0.1f, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, rect, paint);

        return output;
    }

}

now in your layout.xml use below code

<com.example.RoundedImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    app:srcCompat="@drawable/disha" />

enter image description here

like image 39
AskNilesh Avatar answered Oct 05 '22 23:10

AskNilesh