So this is the usual behavior on pre lollipop. Here are the steps to fix it:
Step 1: add the following attributes to your cardView
card_view:cardUseCompatPadding="true" card_view:cardPreventCornerOverlap="false" card_view:cardCornerRadius="10dp"
Step 2: use your own ImageView that rounds the top borders:
public class RoundedTopImageView extends ImageView { private Paint mPaint; private Path mPath; private Bitmap mBitmap; private Matrix mMatrix; private int mRadius = DisplayUtils.convertDpToPixel(10); private int mWidth; private int mHeight; private Drawable mDrawable; public RoundedTopImageView(Context context) { super(context); init(); } public RoundedTopImageView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RoundedTopImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.WHITE); mPath = new Path(); } @Override public void setImageDrawable(Drawable drawable) { mDrawable = drawable; if (drawable == null) { return; } mBitmap = drawableToBitmap(drawable); int bDIWidth = mBitmap.getWidth(); int bDIHeight = mBitmap.getHeight();
Step 3: just replace ImageView in xml with RoundedTopImageView
Step 4: use this in your code as a regular image, for example using Picasso:
RoundedTopImageView image = (RoundedTopImageView) findViewById(R.id.di_iv_image); Picasso.with(context) .load("Some cool Url") .into(image);
EDIT: added convertDpToPixel function
Sorry, I forgot to add this, which is part of the Util class, which you can add anywhere (in my case in the DisplayUtils class):
public static int convertDpToPixel(int dp) { DisplayMetrics displayMetrics = Resources.getSystem().getDisplayMetrics(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics); }
source share