Skip to content

Instantly share code, notes, and snippets.

@gabrielemariotti
Last active February 3, 2026 07:04
Show Gist options
  • Select an option

  • Save gabrielemariotti/f5a176f1b941200fac68 to your computer and use it in GitHub Desktop.

Select an option

Save gabrielemariotti/f5a176f1b941200fac68 to your computer and use it in GitHub Desktop.

Revisions

  1. gabrielemariotti revised this gist Oct 26, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion card_layout.xml
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,5 @@
    android:id="@+id/card_thumbnail_image"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scaleType="centerCrop"
    style="@style/card_thumbnail_image"/>
    </android.support.v7.widget.CardView>
  2. gabrielemariotti created this gist Oct 26, 2014.
    19 changes: 19 additions & 0 deletions MainActivity.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    ImageView imageView = (ImageView) findViewById(R.id.card_thumbnail_image);
    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rose);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
    //Default
    imageView.setBackgroundResource(R.drawable.rose);
    } else {
    //RoundCorners
    RoundCornersDrawable round = new RoundCornersDrawable(mBitmap,
    getResources().getDimension(R.dimen.cardview_default_radius), 0); //or your custom radius

    CardView cardView = (CardView) findViewById(R.id.card_view);
    cardView.setPreventCornerOverlap(false); //it is very important

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
    imageView.setBackground(round);
    else
    imageView.setBackgroundDrawable(round);
    }
    64 changes: 64 additions & 0 deletions RoundCornersDrawable.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    /**
    * Image with rounded corners
    *
    * You can find the original source here:
    * http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
    *
    * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
    */
    public class RoundCornersDrawable extends Drawable {

    private final float mCornerRadius;
    private final RectF mRect = new RectF();
    //private final RectF mRectBottomR = new RectF();
    //private final RectF mRectBottomL = new RectF();
    private final BitmapShader mBitmapShader;
    private final Paint mPaint;
    private final int mMargin;

    public RoundCornersDrawable(Bitmap bitmap, float cornerRadius, int margin) {
    mCornerRadius = cornerRadius;

    mBitmapShader = new BitmapShader(bitmap,
    Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setShader(mBitmapShader);

    mMargin = margin;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    mRect.set(mMargin, mMargin, bounds.width() - mMargin, bounds.height() - mMargin);
    //mRectBottomR.set( (bounds.width() -mMargin) / 2, (bounds.height() -mMargin)/ 2,bounds.width() - mMargin, bounds.height() - mMargin);
    // mRectBottomL.set( 0, (bounds.height() -mMargin) / 2, (bounds.width() -mMargin)/ 2, bounds.height() - mMargin);
    }

    @Override
    public void draw(Canvas canvas) {
    canvas.drawRoundRect(mRect, mCornerRadius, mCornerRadius, mPaint);
    //canvas.drawRect(mRectBottomR, mPaint); //only bottom-right corner not rounded
    //canvas.drawRect(mRectBottomL, mPaint); //only bottom-left corner not rounded

    }

    @Override
    public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
    mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
    }


    }
    14 changes: 14 additions & 0 deletions card_layout.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="250dp"
    android:layout_height="200dp">

    <ImageView
    android:id="@+id/card_thumbnail_image"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:scaleType="centerCrop"
    style="@style/card_thumbnail_image"/>
    </android.support.v7.widget.CardView>