要创建一个带有勾号的圆形形状,你可以使用自定义的Drawable来实现。以下是一个示例代码:
public class CheckCircleDrawable extends Drawable {
private Paint circlePaint;
private Paint tickPaint;
private float strokeWidth;
private int circleColor;
private int tickColor;
public CheckCircleDrawable(float strokeWidth, int circleColor, int tickColor) {
this.strokeWidth = strokeWidth;
this.circleColor = circleColor;
this.tickColor = tickColor;
circlePaint = new Paint();
circlePaint.setAntiAlias(true);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(strokeWidth);
circlePaint.setColor(circleColor);
tickPaint = new Paint();
tickPaint.setAntiAlias(true);
tickPaint.setStyle(Paint.Style.STROKE);
tickPaint.setStrokeWidth(strokeWidth);
tickPaint.setColor(tickColor);
}
@Override
public void draw(@NonNull Canvas canvas) {
Rect bounds = getBounds();
float centerX = bounds.exactCenterX();
float centerY = bounds.exactCenterY();
float radius = Math.min(centerX, centerY) - strokeWidth / 2;
// 绘制圆形
canvas.drawCircle(centerX, centerY, radius, circlePaint);
// 绘制勾号
Path tickPath = new Path();
tickPath.moveTo(centerX - radius / 2, centerY);
tickPath.lineTo(centerX - radius / 8, centerY + radius / 2);
tickPath.lineTo(centerX + radius / 2, centerY - radius / 2);
canvas.drawPath(tickPath, tickPaint);
}
@Override
public void setAlpha(int alpha) {
circlePaint.setAlpha(alpha);
tickPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
circlePaint.setColorFilter(colorFilter);
tickPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSPARENT;
}
}
CheckCircleDrawable checkCircleDrawable = new CheckCircleDrawable(5, Color.BLUE, Color.WHITE);
View view = findViewById(R.id.your_view_id);
view.setBackground(checkCircleDrawable);
这样,你就可以在你的View上显示一个带有勾号的圆形形状了。你可以根据需要调整画笔的宽度、填充颜色和勾号颜色。