In This Tutorial, I will teach you how to Make a Circular Image in Android Studio and Kotlin without using any external library.
You will see that it will be very simple and you will not use any other library.
First of all, open android studio and create a new project. Inside your activity_main.xml add an ImageView and add an Id to this image.
choose an image and copy that image from your desktop to your resource file
Now open your MainActivity.kt and under the setContentView method this line
val img = BitmapFactory.decodeResource(resources,R.drawable.image)
this line will allow you to decode an image into a bitmap file and then we will be able to manipulate it.
now create a new variable and add this line:
val round = RoundedBitmapDrawableFactory.create(resources,img)
now let's add a corner radius value to this image using this line
round.cornerRadius = 20f
and now set the new image source, for this example, I used "image" as an Id and add this line
image.setImageDrawable(round)
now let's run the app and the result will look like this
And now let's see how to make the circular image, for this just replace the cornerRadius line with this
round.isCircular = true;
and if you run the app it will look like this
and that's it 😁
now I will show you the full source code
rounded source code:
package tn.doctorcode.roundimg
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val img = BitmapFactory.decodeResource(resources,R.drawable.image)
val round = RoundedBitmapDrawableFactory.create(resources,img)
// Make a Rounded Image
round.cornerRadius = 20f
image.setImageDrawable(round)
}
}
circle image source code:
package tn.doctorcode.roundimg
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val img = BitmapFactory.decodeResource(resources,R.drawable.image)
val round = RoundedBitmapDrawableFactory.create(resources,img)
// Make a circular Image
round.isCircular = true;
image.setImageDrawable(round)
}
}
How To Make a Circular Image in Android Studio without using any external library
Reviewed by Medics
on
May 03, 2020
Rating:
No comments: