我需要使用canvas创建一个带有两个圆角的矩形视图。我使用了drawRoundRect但是我得到了带有四个圆角的矩形。请任何人建议我一种有助于解决我的问题的方法。
rect = new RectF(left, top, right, bottom);
canvas.drawRoundRect(rect, 20, 20, facePaint);
答案 0 :(得分:1)
有点浪费,但首先绘制一个圆角矩形,所有4个角都是圆角,然后在下方绘制第二个规则矩形radius
高度以绘制底部圆角
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float radius = 20f;
@Override
public void onDraw(Canvas canvas) {
// Draw the top part that has rounded corners with twice the height of the radius
canvas.drawRoundRect(0f, 0f, 100f, 2 * radius, radius, radius, paint);
// Draw the bottom part, partly on top of the top part
canvas.drawRect(0f, radius, 100f, 100f, paint);
}
需要一点点数学来解释所需的高度,但不应该那么难:)
答案 1 :(得分:0)
没有内置功能可以做到这一点。您需要创建一个具有所需形状的路径,然后使用drawPath绘制画布的路径
答案 2 :(得分:0)
只需转到您的drawable文件夹添加新的xml文件并添加以下代码: -
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="20dp"/>
<solid android:color="@color/green" />
</shape>
在此之后设置任何视图的背景: -
android:background="@drawable/round_green"
您可以根据自己的选择更改颜色。
希望它会对你有所帮助。
答案 3 :(得分:0)
使用: -
// Initialize a new Bitmap
Bitmap bitmap = Bitmap.createBitmap(
600, // Width
300, // Height
Bitmap.Config.ARGB_8888 // Config
);
// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);
// Draw a solid color on the canvas as background
canvas.drawColor(Color.WHITE);
// Initialize a new Paint instance to draw the rounded rectangle
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setAntiAlias(true);
// Set an offset value in pixels to draw rounded rectangle on canvas
int offset = 50;
/*
public RectF (float left, float top, float right, float bottom)
Create a new rectangle with the specified coordinates. Note: no range
checking is performed, so the caller must ensure that
left <= right and top <= bottom.
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X coordinate of the right side of the rectangle
bottom The Y coordinate of the bottom of the rectangle
*/
// Initialize a new RectF instance
RectF rectF = new RectF(
offset, // left
offset, // top
canvas.getWidth() - offset, // right
canvas.getHeight() - offset // bottom
);
/*
public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
Draw the specified round-rect using the specified paint. The roundrect
will be filled or framed based on the Style in the paint.
Parameters
rect : The rectangular bounds of the roundRect to be drawn
rx : The x-radius of the oval used to round the corners
ry : The y-radius of the oval used to round the corners
paint : The paint used to draw the roundRect
*/
// Define the corners radius of rounded rectangle
int cornersRadius = 25;
// Finally, draw the rounded corners rectangle object on the canvas
canvas.drawRoundRect(
rectF, // rect
cornersRadius, // rx
cornersRadius, // ry
paint // Paint
);
// Display the newly created bitmap on app interface
mImageView.setImageBitmap(bitmap);
答案 4 :(得分:0)
drawRoundRect
的来源。所以,如果你想绘制两个圆角。
public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
你可以尝试这个。只剩下和顶部有圆角。
canvas.drawRoundRect(10,10,0,0,20,20,facePaint);
答案 5 :(得分:0)
您可以使用xml drawable制作2个圆角矩形。
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp"></corners>
<solid android:color="@color/white"></solid>
</shape>