如何切割半圆形的矩形拉丝?

时间:2014-01-08 10:16:01

标签: android android-canvas

我需要帮助切割半圆形的矩形拉丝。

我尝试使用Path.addArc()但无法获得所需的结果。我只能用矩形切割图像。

Path path = new Path();
path.addArc(rectF, 0, 180);
canvas.clipPath(path, Region.Op.DIFFERENCE);
canvas.drawBitmap(orig, rect, rect, circlePaint);

1 个答案:

答案 0 :(得分:0)

您需要使用此方法从矩形位图

获取圆形
public static Bitmap getCircularBitmap(Bitmap bitmap) {
    Bitmap output;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }
    RadialGradient gradient = new RadialGradient(bitmap.getWidth() / 2, bitmap.getWidth() / 2, bitmap.getWidth() / 2, 
            new int[] {0xFFFFFFFF, 0xFFFFFFFF, 0x00FFFFFF},
            new float[] {0.0f, 0.4f, 0.8f}, 
            android.graphics.Shader.TileMode.CLAMP); 

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    paint.setShader(gradient); 
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

然后你可以像这样将圆圈切成两半

 Bitmap semicircle=Bitmap.createBitmap(circlebitmap, 0, 0, circlebitmap.getWidth()/2 , circlebitmap.getHeight());