Этот прямоугольник с закругленными углами относится к This. Затем я кое-что изменю.
При этом я хочу добавить хвост в правом нижнем и левом нижнем углу в зависимости от требований использования, поэтому может ли кто-нибудь помочь мне завершить это iOS-подобное Облако сообщений в чате::
Код: Выделить всё
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;
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... in-android