5

我可以画出一个有锋利边缘的矩形,现在我需要把锋利的边缘弄圆。

怎么做?

这是我的代码:

 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);
   }

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

4

3 回答 3

27

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

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

希望它会帮助别人。

于 2013-02-07T10:35:52.320 回答
6

您可以使用drawRoundRect

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

于 2013-02-07T09:51:26.117 回答
-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;
    }
于 2013-02-07T09:52:31.973 回答