画圆角方形

时间:2013-02-07 09:47:11

标签: android canvas draw

我能够绘制一个锋利边缘的矩形,现在我需要使锋利的边缘变圆。

怎么做?

这是我的代码:

 public void drawShape(Canvas canvas, Renderer renderer, float x, float y,
      int seriesIndex, Paint paint) {
    float halfShapeWidth = shape_width / 2;
    canvas.drawRect(x , y - halfShapeWidth, x + SHAPE_WIDTH, y + halfShapeWidth, paint);
   }

如何通过传递相同的参数来制作这个圆角矩形?

3 个答案:

答案 0 :(得分:27)

好的,我用自己的代码解决了这个问题:

RectF r = new RectF(1,2,3,4);
canvas.drawRoundRect(r, 0, 0, mPaint);

希望它能帮助别人。

答案 1 :(得分:6)

您可以使用drawRoundRect

在调用此函数之前,您需要将位置和尺寸打包在RectF中。

答案 2 :(得分:-5)

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }