How to create a Circular image view in Android without using any library?

How to create a Circular image view in Android without using any library?

Creating a circular ImageView without using any library requires you to create a custom Drawable or use a CardView with rounded corners. Here's how you can achieve it:

Method 1: Using a Custom Drawable

  • Create a custom BitmapDrawable:

Create a new Kotlin file CircularBitmapDrawable.kt and add the following code:

import android.graphics.* import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.Drawable class CircularBitmapDrawable(private val bitmap: Bitmap) : Drawable() { private val paint = Paint().apply { isAntiAlias = true shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) } override fun draw(canvas: Canvas) { val cx = bounds.width() / 2f val cy = bounds.height() / 2f val radius = Math.min(cx, cy) canvas.drawCircle(cx, cy, radius, paint) } override fun setAlpha(alpha: Int) { paint.alpha = alpha } override fun setColorFilter(cf: ColorFilter?) { paint.colorFilter = cf } override fun getOpacity(): Int { return PixelFormat.TRANSLUCENT } override fun getIntrinsicWidth(): Int { return bitmap.width } override fun getIntrinsicHeight(): Int { return bitmap.height } } 
  • Use the custom drawable:
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.your_image) val circularBitmapDrawable = CircularBitmapDrawable(bitmap) imageView.setImageDrawable(circularBitmapDrawable) 

Method 2: Using CardView with Rounded Corners

  • Add CardView Dependency:

If you haven't added it yet, include the CardView dependency in your build.gradle (Module: app):

implementation 'androidx.cardview:cardview:1.0.0' 
  • Use CardView in your layout:

In your XML layout, wrap the ImageView with CardView:

<androidx.cardview.widget.CardView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" app:cardCornerRadius="50dp" app:cardElevation="0dp" app:cardBackgroundColor="@android:color/transparent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/your_image" android:scaleType="centerCrop"/> </androidx.cardview.widget.CardView> 

The key here is setting the app:cardCornerRadius to half of the width/height of the CardView to make it circular.

These methods allow you to achieve a circular ImageView effect without relying on third-party libraries, giving you more control over the rendering process.

Examples

  1. Create Circular ImageView in Android programmatically:

    • Description: To create a Circular ImageView programmatically, you need to set the background to an oval shape and clip the ImageView to its outline.

    • Code:

      ImageView circularImageView = new ImageView(context); circularImageView.setLayoutParams(new ViewGroup.LayoutParams(150, 150)); circularImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); circularImageView.setImageResource(R.drawable.profile_image); // Set circular shape programmatically circularImageView.setBackground(new ShapeDrawable(new OvalShape())); circularImageView.setClipToOutline(true); // Add the ImageView to the parent layout parentLayout.addView(circularImageView); 
  2. How to make a circular image view in Android:

    • Description: Making a Circular ImageView involves setting the background shape to an oval and clipping it to the outline.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/circular_shape" /> 
      • res/drawable/circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#000000" /> </shape> 
  3. Circular image view in Android without external library:

    • Description: Creating a Circular ImageView without using an external library can be achieved using standard Android features.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/circular_shape" /> 
      • res/drawable/circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#000000" /> </shape> 
  4. Android ImageView circular shape without library:

    • Description: Achieving a circular shape for an ImageView without using an external library involves defining a circular shape in XML.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/circular_shape" /> 
      • res/drawable/circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#000000" /> </shape> 
  5. Draw circular shape around ImageView in Android:

    • Description: Drawing a circular shape around an ImageView involves using an oval shape as the background.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/circular_shape" /> 
      • res/drawable/circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#000000" /> </shape> 
  6. Programmatic approach to circular ImageView in Android:

    • Description: Programmatically creating a Circular ImageView involves setting the background shape to an oval and clipping it to the outline.

    • Code:

      ImageView circularImageView = new ImageView(context); circularImageView.setLayoutParams(new ViewGroup.LayoutParams(150, 150)); circularImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); circularImageView.setImageResource(R.drawable.profile_image); // Set circular shape programmatically circularImageView.setBackground(new ShapeDrawable(new OvalShape())); circularImageView.setClipToOutline(true); // Add the ImageView to the parent layout parentLayout.addView(circularImageView); 
  7. Implement circular image view without third-party library in Android:

    • Description: Implementing a Circular ImageView without using a third-party library involves setting the background to an oval shape and clipping it to the outline.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/circular_shape" /> 
      • res/drawable/circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#000000" /> </shape> 
  8. Customize circular shape for ImageView in Android:

    • Description: Customize the circular shape for an ImageView by adjusting the attributes in the oval shape XML.

    • Code:

      <ImageView android:id="@+id/circularImageView" android:layout_width="150dp" android:layout_height="150dp" android:scaleType="centerCrop" android:src="@drawable/profile_image" android:background="@drawable/custom_circular_shape" /> 
      • res/drawable/custom_circular_shape.xml:
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#FF4081" /> </shape> 
  9. Code example for creating circular ImageView in Android:

    • Description: A complete code example for creating a Circular ImageView programmatically.

    • Code:

      ImageView circularImageView = new ImageView(context); circularImageView.setLayoutParams(new ViewGroup.LayoutParams(150, 150)); circularImageView.setScaleType(ImageView.ScaleType.CENTER_CROP); circularImageView.setImageResource(R.drawable.profile_image); // Set circular shape programmatically circularImageView.setBackground(new ShapeDrawable(new OvalShape())); circularImageView.setClipToOutline(true); // Add the ImageView to the parent layout parentLayout.addView(circularImageView); 

More Tags

cron-task android-logcat android-developer-api protoc beagleboneblack phpexcel karel mocking store sqldatasource

More Programming Guides

Other Guides

More Programming Examples