1

I have a progressbar and I used RoundedBitmapDrawable to pass a drawable as background, but how can I make only two corners round? If I use a xml as background I get no background.

Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.progressbar);

RoundedBitmapDrawable dr = RoundedBitmapDrawableFactory.create(getResources(), src);

dr.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f);
Maria Georgali
  • 629
  • 1
  • 9
  • 22
  • no, you have write your own `Drawable` based on `RoundedBitmapDrawable` code – pskink Apr 20 '17 at 09:39
  • https://developer.android.com/reference/android/support/v4/graphics/drawable/RoundedBitmapDrawable.html there is no such a utility provided – Akshay Apr 20 '17 at 09:42

1 Answers1

1

You can try doing this:

  1. Copy android.support.v4.graphics.drawable.RoundedBitmapDrawable and android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory to your project, with different names (don't forget to change the package name and fix all imports/etc)
  2. Edit the public void draw(Canvas canvas) method by replacing the drawRoundRect method call (line 264) with one of the solutions from this thread.
  3. Use your new class!

Tried adapting Durza007's solution, which is the easiest, however it didn't work for me.

I ended up using Mohammad Mahtabi's solution: added his public static Path RoundedRect() to my CustomRoundedBitmapDrawable class, and replaced drawRoundRect with

Path path = RoundedRect(0, 0, mDstRect.width() , mDstRect.height(),  
    mCornerRadius, mCornerRadius, true, true, false, false);
canvas.drawPath(path, mPaint);

Here, true, true, false, false means that the top left and right corners are rounded. It would be wise to make these parameters configurable in HalfRoundedBitmapDrawable to reuse the class later on.

Tested myself on 4.4 (KitKat), works like a charm.

kit
  • 445
  • 2
  • 5
  • 19