1

Actually I wanna convert the image into the mentioned shape in picture . I've tried a lot of similar questions in SO but none of them were helpful. You may advise to use drawable in foreground of imageView but I've gotta set the foreground programmatically which is valid only api>23 . Can anyone please help me to solve this issue?

You can see in image that there's background of image . I wanna convert the image into that shape programmatically.

Even I tried to use Gradient Drawable but it was futile

Code:

GradientDrawable gradientDrawable= new GradientDrawable();
        float[] radii = new float[8];
        radii[0] = 15; // Goes clockwise, so this is the top left corner start position.
        radii[1] = 15; // top left end position.
        radii[2] = 15;
        radii[3] = 15;
        radii[4]=0;
        radii[5]=0;
        radii[6] = 15;
        radii[7] = 15;
        gradientDrawable.setCornerRadii(radii);
        holder.img.setBackgroundDrawable(gradientDrawable);

Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45

3 Answers3

1

You could change the onDraw method of your imageView class by applying a clipPath:

@Override
protected void onDraw(Canvas canvas) {
    float[] radii = new float[8];
    radii[0] = radius; // Goes clockwise, so this is the top left corner start position.
    radii[1] = radius; // top left end position.
    radii[2] = radius;
    radii[3] = radius;
    // Skipping 4 and 5 because thats the bottom right corner.
    radii[6] = radius;
    radii[7] = radius;
    Path clipPath = new Path();
    clipPath.addRoundRect(new RectF(0, 0, this.getWidth(), this.getHeight()), radii, Path.Direction.CW);
    canvas.clipPath(clipPath);
    super.onDraw(canvas);
}

refs:

wolfenrain
  • 309
  • 1
  • 9
0

If you want to create a drawable image then you can set a different radius for a different corner like

<corners
    android:topRightRadius="15dp"
    android:topLeftRadius="15dp"
    android:bottomLeftRadius="15dp"/>

Apart from that, you can use GradientDrawable to create a background image and use setCornerRadii for variable corner radius.

Example for GradientDrawable

GradientDrawable gradientDrawable= new GradientDrawable();
gradientDrawable.setCornerRadii(15,15,15,15,15,15,0,0);

void setCornerRadii (float[] radii)

Specifies radii for each of the 4 corners. For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.

for setting color use gradientDrawable.setColor(YOUR_COLOR) method and you can use multiple color there for gradient look.

Here (15,15,15,15,15,15,0,0) suggest X radius and Y radius for all 4 corner starting from top left.

Kunu
  • 5,078
  • 6
  • 33
  • 61
0

Finally I solved my problem by taking reference from from this and this

Finally the ImageView Class looks like:

public class RoundedImageView extends ImageView {

private Path mMaskPath;
private Paint mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mCornerRadius = 10;

public RoundedImageView(Context context) {
    super(context);

    init();
}

public RoundedImageView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

    init();
}

public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    init();
}

private void init() {
    ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null);
    mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}


public void setCornerRadius(int cornerRadius) {
    mCornerRadius = cornerRadius;
    generateMaskPath(getWidth(), getHeight());
    invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
    super.onSizeChanged(w, h, oldW, oldH);

    if (w != oldW || h != oldH) {
        generateMaskPath(w, h);
    }
}

private void generateMaskPath(int w, int h) {
    mMaskPath = new Path();
    float[] radii = new float[8];
    radii[0] = 20; // Goes clockwise, so this is the top left corner start position.
    radii[1] = 20; // top left end position.
    radii[2] = 20;
    radii[3] = 20;
    // Skipping 4 and 5 because thats the bottom right corner.
    radii[6] = 20;
    radii[7] = 20;
    mMaskPath.addRoundRect(new RectF(0,0,w,h), radii, Path.Direction.CW);
    mMaskPath.setFillType(Path.FillType.INVERSE_WINDING);
}

@SuppressLint("WrongConstant")
@Override
protected void onDraw(Canvas canvas) {
    if(canvas.isOpaque()) { // If canvas is opaque, make it transparent
        canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
    }

    super.onDraw(canvas);

    if(mMaskPath != null) {
        canvas.drawPath(mMaskPath, mMaskPaint);
    }
}
 }
Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45