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>
*Note: it requires at least the version 1.2.0-alpha03
.
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;
}
}
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 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 );
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:
That's it Happy Coding :).
ImageView rejecting rounded corners and borders of parent
Make all the corners of imageview rounded. Bottom of that will be hide back of linearlayout.
Android ImageView with Rounded Corners not working
You're lacking a couple of tags. Here's a new sample:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="30px"/>
</shape>
Seen here
Also, are you aware of RoundRectShape?
Related Topics
How to Get an Android User's Email Address
How to Programmatically Enable Gps in Android Cupcake
Androidruntime Error: Parcel: Unable to Marshal Value
How to Disable Sslv3 in Android for Httpsurlconnection
What Is Fatal Signal 6 in Android Logcat
Pausing/Stopping and Starting/Resuming Java Timertask Continuously
Understand Arraylist Indexoutofboundsexception in Android
How to Give Images Rounded Corners in Android
How to Allow Users to Check for the Latest App Version from Inside the App
Android Data Binding Using Include Tag
Change Position of Google Maps API's "My Location" Button
How to Change Color of Hamburger Icon in Material Design Navigation Drawer
Using File.Listfiles with Filenameextensionfilter