I am beginner to Android. Now I am learning how to design Android App properly. But I am having a problem with ImageView. I want to round the corners of ImageView but only for one side.
This image is what I want to get:
As you can see only left side corners of ImageView are rounded fitting to parent view.
This is what I am getting right now:
This is the XML layout for my list view row:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/list_row_bg"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:background="@color/colorPrimary"
android:id="@+id/row_image"
android:scaleType="fitCenter"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
<LinearLayout
android:padding="@dimen/list_item_content_padding"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/row_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/row_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/row_file_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/row_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I followed this tutorial. But the padding is included for ImageView. But when I remove padding, it is not working properly.
I think that you should apply a transformation when loading the images. For example if you use picasso for loading imageas you could apply this transformation:
Base class:
public class RoundedCornersBitmap implements Transformation {
private static final float DEFAULT_RADIUS = 5.f;
private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
private static final int DEFAULT_STROKE_WIDTH = 3;
protected float mCornerRadius;
protected int mBorderColor;
protected int mStrokeWidth;
@Override
public String key() {
return "roundedCorners()";
}
public RoundedCornersBitmap() {
mCornerRadius = DEFAULT_RADIUS;
mBorderColor = DEFAULT_BORDER_COLOR;
mStrokeWidth = DEFAULT_STROKE_WIDTH;
}
public RoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
mCornerRadius = cornderRadius;
mBorderColor = borderColor;
mStrokeWidth = strokeWidth;
}
@Override
public Bitmap transform(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(mStrokeWidth, mStrokeWidth, (bitmap.getWidth() - mStrokeWidth), bitmap.getHeight()
- mStrokeWidth);
final RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
paint.setColor(mBorderColor);
paint.setStrokeWidth(3);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
//canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
/**
* @return the mCornerRadius
*/
public float getCornerRadius() {
return mCornerRadius;
}
/**
* @param mCornerRadius
* the mCornerRadius to set
*/
public void setCornerRadius(float mCornerRadius) {
this.mCornerRadius = mCornerRadius;
}
/**
* @return the mBorderColor
*/
public int getBorderColor() {
return mBorderColor;
}
/**
* @param mBorderColor
* the mBorderColor to set
*/
public void setBorderColor(int mBorderColor) {
this.mBorderColor = mBorderColor;
}
/**
* @return the mStrokeWidth
*/
public int getStrokeWidth() {
return mStrokeWidth;
}
/**
* @param mStrokeWidth
* the mStrokeWidth to set
*/
public void setStrokeWidth(int mStrokeWidth) {
this.mStrokeWidth = mStrokeWidth;
}
}
LeftRoundedCornersBitmap:
public class LeftRoundedCornersBitmap extends RoundedCornersBitmap {
public LeftRoundedCornersBitmap() {
super();
}
public LeftRoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
super(cornderRadius, borderColor, strokeWidth);
}
@Override
public Bitmap transform(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(mStrokeWidth, mStrokeWidth, (bitmap.getWidth() - mStrokeWidth), bitmap.getHeight()
- mStrokeWidth);
final RectF rectF = new RectF(rect);
final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2);
final Rect bottomRect = new Rect(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
paint.setColor(mBorderColor);
paint.setStrokeWidth(3);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
canvas.drawRect(topRightRect, paint);
canvas.drawRect(bottomRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
Picasso dependency in the gradle file:
compile 'com.squareup.picasso:picasso:2.5.2'
Example:
Picasso.with(context).load(imageUrl).transform(new LeftRoundedCornersBitmap()).into(youImageView);