fill Rectangle on Canvas - Android android.graphics

Android examples for android.graphics:Canvas

Description

fill Rectangle on Canvas

Demo Code

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Main {

  private static Paint paint = new Paint();

  public static void fillRect(Canvas canvas, int left, int top, int right, int bottom, boolean drawBorder,
      int fillColor, int borderColor, float borderSize) {
    if (borderSize == 0)
      borderSize = 0.5f;/*from  w w  w .  j  a v a 2s. c om*/
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    canvas.save();
    canvas.clipRect(new Rect(left, top, right, bottom));
    if (drawBorder) {
      paint.setColor(borderColor);
      canvas.drawRect(left, top, right, bottom, paint);
      paint.setColor(fillColor);
      canvas.drawRect(left + borderSize, top + borderSize, right - (borderSize * 2), bottom - (borderSize * 2), paint);
    } else {
      paint.setColor(fillColor);
      canvas.drawRect(left, top, right, bottom, paint);
    }
    canvas.restore();

  }

  public static void fillRect(Canvas canvas, Rect rect, boolean drawBorder, int fillColor, int borderColor,
      float borderSize) {
    fillRect(canvas, rect.left, rect.top, rect.right, rect.bottom, drawBorder, fillColor, borderColor, borderSize);
  }

}

Related Tutorials