Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
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:
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 } }
val bitmap = BitmapFactory.decodeResource(resources, R.drawable.your_image) val circularBitmapDrawable = CircularBitmapDrawable(bitmap) imageView.setImageDrawable(circularBitmapDrawable)
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'
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.
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);
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>
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>
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>
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>
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);
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>
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>
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);