create Round Rectangle Path - Android android.graphics

Android examples for android.graphics:Path

Description

create Round Rectangle Path

Demo Code

import android.graphics.Path;
import android.graphics.RectF;

public class Main {

  public static Path createRoundRectPath(float aWidth, float aHeight, float aXPadding, float aYPadding,
      float aCornerSize, boolean isRound1, boolean isRound2, boolean isRound3, boolean isRound4) {
    return createRoundRectPath(aWidth, aHeight, aXPadding, aXPadding, aYPadding, aYPadding, aCornerSize, isRound1,
        isRound2, isRound3, isRound4);/*  ww  w  .  ja  va 2s. c  om*/
  }

  public static Path createRoundRectPath(float aWidth, float aHeight, float aPaddingLeft, float aPaddingRight,
      float aPaddingTop, float aPaddingBottom, float aCornerSize, boolean isRound1, boolean isRound2, boolean isRound3,
      boolean isRound4) {
    Path path = new Path();
    RectF oval;

    if (isRound1) {
      path.moveTo(aPaddingLeft, aPaddingTop + aCornerSize);
      oval = new RectF(aPaddingLeft, aPaddingTop, aPaddingLeft + aCornerSize * 2, aPaddingTop + aCornerSize * 2);
      path.arcTo(oval, 180, 90);
    } else {
      path.moveTo(aPaddingLeft, aPaddingTop);
    }

    if (isRound2) {
      path.lineTo(aWidth - aPaddingRight - aCornerSize, aPaddingTop);
      oval = new RectF(aWidth - aPaddingRight - aCornerSize * 2, aPaddingTop, aWidth - aPaddingRight,
          aPaddingTop + aCornerSize * 2);
      path.arcTo(oval, 270, 90);
    } else {
      path.lineTo(aWidth - aPaddingRight, aPaddingTop);
    }

    if (isRound3) {
      path.lineTo(aWidth - aPaddingRight, aHeight - aPaddingBottom - aCornerSize);
      oval = new RectF(aWidth - aPaddingRight - aCornerSize * 2, aHeight - aPaddingBottom - aCornerSize * 2,
          aWidth - aPaddingRight, aHeight - aPaddingBottom);
      path.arcTo(oval, 0, 90);
    } else {
      path.lineTo(aWidth - aPaddingRight, aHeight - aPaddingBottom);
    }

    if (isRound4) {
      path.lineTo(aPaddingLeft + aCornerSize, aHeight - aPaddingBottom);
      oval = new RectF(aPaddingLeft, aHeight - aPaddingBottom - aCornerSize * 2, aPaddingLeft + aCornerSize * 2,
          aHeight - aPaddingBottom);
      path.arcTo(oval, 90, 90);
    } else {
      path.lineTo(aPaddingLeft, aHeight - aPaddingBottom);
    }

    path.close();

    return path;
  }

}

Related Tutorials