22 May 2024 Leave a comment Tech-Help
In Android, an ImageView is a rectangle by default. If you want to make it a rounded rectangle, there are several approaches you can take. This guide will walk you through the most effective methods to achieve rounded corners on your ImageView, based on the latest and most relevant solutions available.
Using ShapeableImageView
Starting with version 1.2.0-alpha03 of the Material Components Library, you can easily make rounded corners using ShapeableImageView
. This approach is straightforward and leverages the latest advancements in the Android ecosystem.
XML Implementation
First, add the ShapeableImageView
to your layout XML file:
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:shapeAppearanceOverlay="@style/roundedImageView"
app:srcCompat="@drawable/ic_image" />
Next, define the rounded corners in your themes.xml
:
<style name="roundedImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">8dp</item>
</style>
Programmatic Implementation
You can also set the rounded corners programmatically:
float radius = getResources().getDimension(R.dimen.default_corner_radius);
imageView.setShapeAppearanceModel(imageView.getShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED, radius)
.build());
Alternative Methods
If you are working with older versions of Android or prefer different methods, here are some other approaches:
Using CardView
Another simple way is to wrap your ImageView
in a CardView
and set the corner radius:
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="8dp"
android:layout_margin="5dp"
android:elevation="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image"
android:background="@color/white"
android:scaleType="centerCrop" />
</androidx.cardview.widget.CardView>
Using RoundedBitmapDrawable
For API level 21 and above, you can use RoundedBitmapDrawable
:
RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(getResources(), src);
dr.setCornerRadius(cornerRadius);
imageView.setImageDrawable(dr);
Conclusion
These methods provide you with various options to create rounded corners on an ImageView in Android. Depending on your project requirements and API levels, you can choose the most suitable approach.
Enhancing Your Development with Repeato
As you focus on creating visually appealing and functional mobile applications, remember that testing is a crucial part of the development process. Our product, Repeato, is a no-code test automation tool for iOS and Android. It allows you to create, run, and maintain automated tests for your apps efficiently.
Repeato is particularly beneficial for mobile developers as it enables them to concentrate on creating a great product instead of spending excessive time on test automation. Additionally, it allows developers to delegate test automation tasks to non-technical colleagues or QA teams.
For more information on how Repeato can streamline your mobile app testing, visit our blog or check out our documentation.