How to Make an Imageview With Rounded Corners

How to make an ImageView with rounded corners?

This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.

http://www.ruibm.com/?p=184

This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.

Final code looks like this:

package com.company.app.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageHelper {
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}
}

How to make an ImageView with rounded corners?

This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.

http://www.ruibm.com/?p=184

This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.

Final code looks like this:

package com.company.app.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageHelper {
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
}
}

Imageview not showing rounded corners

Just use the ShapeableImageView in the Material Components library.

Something like:

  <com.google.android.material.imageview.ShapeableImageView
...
app:shapeAppearanceOverlay="@style/roundedCornersImageView"
app:srcCompat="@drawable/ic_image" />

with:

  <style name="roundedCornersImageView" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">xxdp</item>
</style>

Sample Image

*Note: it requires at least the version 1.2.0-alpha03.

How to quickly make ImageViews with rounded corners?

The solution is pretty simple actually, but yet hasn't been suggested that much.
A few months ago I was searching for a simple solution to get a rounded ImageView, and all I could be able to find was complicated solutions.
I'm writing this answer, since this is not known by everybody (and this solution has been implemented in Android at the end of 2020), so that if someone new to Android Studio will have the same question will be able to quickly find a simple solution to make any ImageView rounded.

First thing first, since the solution is implemented in an androidx library, you should make sure that you're declaring this dependency in your gradle:

dependencies {
//...
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
}

After syncing your gradle, you will be able to use a component called "ImageFilterView".

ImageFilterView is sub-class of ImageView, so it inherits everything from a common ImageView, but there are a few things added in order to manipulate the image.
Among these new things, there is the attribute "roundPercent" which allows you to set how much the corners of the image should be rounded.
Remember that this is a percentage value, so it must be a float between 0.0 and 1.0 included.
Settings this attribute to 1.0 will make the image totally circular.

That's all, nothing else to do, this will be enough to get a rounded ImageView:

<androidx.constraintlayout.utils.widget.ImageFilterView
app:roundPercent = 1.0 />

(Remember that you have to define the "app" namespace at the beginning of the first layout of your xml file in order to be able to use it:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- ... -->
</layout>

Rounded corner image view with a custom view

First of all, you are not drawing on the view's canvas, you've to override onDraw to draw anything on the view's canvas. And, as you just want to make your image circular from the corners, you don't need PorterDuff for this. You can just clip a rounded rect from the canvas and it would be suffice for your use case.

You don't have to manually override each constructor of the AppCompatImageView, You can use the @JvmOverloads annotation to override every constructor of the java class in kotlin.

Use withStyledAttributes extension function of core-ktx to access attributeSet

init block can be used to execute code just after the primary constructor.

Don't do object allocation inside of onDraw, reuse expensive objects like paint and path as much as possible.

Keeping above points in mind, your class can be changed like this

class RoundedImageView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet,
defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {
val path = Path()
var cornerRadius: Float = 0f

init {
context.withStyledAttributes(attrs, R.styleable.RoundedImageView) {
cornerRadius = getDimension(R.styleable.RoundedImageView_cornerRadius, 0F)
}
}

override fun onDraw(canvas: Canvas?) {
val corners = floatArrayOf(
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius,
cornerRadius
)

path.addRoundRect(
0f,
0f,
width.toFloat(),
height.toFloat(),
corners,
Path.Direction.CW
)

canvas?.clipPath(path)
super.onDraw(canvas)
}
}

Now, it can be used like this

<com.sambhav2358.facebookclone.customviews.RoundedImageView
android:layout_width="200dp"
android:layout_height="200dp"
app:cornerRadius="70dp"
android:src="@drawable/ic_launcher_background"
/>

ImageView with only bottom or top corners rounded

Here is the another way to do this using Material Design ShapeableImageView

Create one theme for shape and cornerFamily

<style name="ImageView.Corner" parent="">
<item name="cornerSizeTopRight">8dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerFamily">rounded</item>
</style>

Now add ShapeableImageView in XML:

<com.google.android.material.imageview.ShapeableImageView
android:layout_width="75dp"
android:layout_height="75dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/temp_product_image"
app:shapeAppearanceOverlay="@style/ImageView.Corner"/>

I you want to full rounded ShapeableImageView:

<style name="ImageView.Round" parent="">
<item name="cornerSize">50%</item>
</style>

Full Rounded Output:

Sample Image

That's it Happy Coding :).

ImageView rounded corners

I use Universal Image loader library to download and round the corners of image, and it worked for me.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(thisContext)
// You can pass your own memory cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.build();

DisplayImageOptions options = new DisplayImageOptions.Builder()
.displayer(new RoundedBitmapDisplayer(10)) //rounded corner bitmap
.cacheInMemory(true)
.cacheOnDisc(true)
.build();

ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
imageLoader.displayImage(image_url,image_view, options );


Related Topics



Leave a reply



Submit