I want to build the iOS-like Messages chat bubble in Android using the custom view class this is the code that I use to create the rounded corner rectangle and it will be based on the height and weight it will decide the corner radius of all sides.
This rounder corner rectangle refers to This. Then I will modify some.
In this I want to add the tail at the bottom right side and bottom left side based on the use requirement so can anyone help me to complete this iOS-like Messages chat bubble::
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.jetbrains.annotations.Contract;
public class Bubble extends View {
private Paint paint;
private Path path;
public Bubble(Context context) {
super(context);
init();
}
public Bubble(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public Bubble(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public Bubble(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
paint = new Paint();
paint.setColor(Color.DKGRAY);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
RectF rect = new RectF(50f, 50f, 200f, 100f);
path = getPathOfRoundedRectF(rect, 50f);
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
}
@NonNull
public static Path getPathOfRoundedRectF(
@NonNull RectF rect,
float radius
) {
float width = rect.right - rect.left;
float height = rect.bottom - rect.top;
float maxRadius = Math.min(width / 2, height / 2);
float finalRadius = Math.min(radius, maxRadius);
Path path = new Path();
path.moveTo(rect.left + finalRadius, rect.top);
// Top border
path.lineTo(rect.right - finalRadius, rect.top);
// Top-right corner
path.arcTo(
new RectF(
rect.right - finalRadius * 2,
rect.top,
rect.right,
rect.top + finalRadius * 2
), -90f, 90f
);
// Right border
path.lineTo(rect.right, rect.bottom - finalRadius);
// Bottom-right corner
path.arcTo(
new RectF(
rect.right - finalRadius * 2,
rect.bottom - finalRadius * 2,
rect.right,
rect.bottom
), 0f, 90f
);
// Bottom border
path.lineTo(rect.left + finalRadius, rect.bottom);
// Bottom-left corner
path.arcTo(
new RectF(
rect.left,
rect.bottom - finalRadius * 2,
rect.left + finalRadius * 2,
rect.bottom
), 90f, 90f
);
// Left border
path.lineTo(rect.left, rect.top + finalRadius);
// Top-left corner
path.arcTo(
new RectF(
rect.left,
rect.top,
rect.left + finalRadius * 2,
rect.top + finalRadius * 2
), 180f, 90f
);
path.close();
return path;
}
}
I wnat the best solution code I want to add the tail at the bottom right side and bottom left side based on the use requirement so can anyone help me to complete this iOS-like Messages chat bubble.
Jay Dolar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.