Hello Friends, Today I am going to share how to add ImageView with Rounded Corners in Android. Actually recently I was working on an app in which I had to set profile pic of user but with rounded corners. I struggled a lot for this simple thing, but found the solution. So now I am sharing this code to help others as well as myself for future. There is code for both local and online image resources for making it round.
So, here is the code.
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.zatackcoder.roundimage; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.drawable.RoundedBitmapDrawable; import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageLocal = findViewById(R.id.image_local); ImageView imageOnline = findViewById(R.id.image_online); //for local image resource Bitmap batmapBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gull); RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), batmapBitmap); circularBitmapDrawable.setCircular(true); imageLocal.setImageDrawable(circularBitmapDrawable); //for online image resource new LoadAndShowImage(imageOnline).execute("https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg"); } class LoadAndShowImage extends AsyncTask<String, Void, RoundedBitmapDrawable> { ImageView imageView; public LoadAndShowImage(ImageView imageView) { this.imageView = imageView; } protected RoundedBitmapDrawable doInBackground(String... urls) { String imageURL = urls[0]; RoundedBitmapDrawable roundedBitmapDrawable = null; try { InputStream in = new java.net.URL(imageURL).openStream(); Bitmap bimage = BitmapFactory.decodeStream(in); roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bimage); roundedBitmapDrawable.setCircular(true); } catch (Exception e) { Log.e("LoadAndShowImage", e.getMessage()); } return roundedBitmapDrawable; } protected void onPostExecute(RoundedBitmapDrawable result) { imageView.setImageDrawable(result); } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Local Image"/> <ImageView android:id="@+id/image_local" android:layout_width="200dp" android:layout_height="200dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Online Image"/> <ImageView android:id="@+id/image_online" android:layout_width="200dp" android:layout_height="200dp"/> </LinearLayout> |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.roundimage"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
Screenshot
AndroidStudio Project Download
ImageView with Rounded Corners in Android
1 file(s) 11.69 MB
Thanks for Stoping by
If you find this helpful then please share
Comments