Skip to main content
almarefa.net

Back to all posts

How to Draw Rectangles Over Imageview In Kotlin?

Published on
5 min read
How to Draw Rectangles Over Imageview In Kotlin? image

Best Tools and Resources to Draw Overlays on ImageViews in Kotlin to Buy in October 2025

1 Prina 50 Pack Drawing Set Sketch Kit, Sketching Supplies with 3-Color Sketchbook, Graphite, and Charcoal Pencils, Pro Art Drawing Kit for Artists Adults Teens Beginner Kid, Ideal for Shading, Blending

Prina 50 Pack Drawing Set Sketch Kit, Sketching Supplies with 3-Color Sketchbook, Graphite, and Charcoal Pencils, Pro Art Drawing Kit for Artists Adults Teens Beginner Kid, Ideal for Shading, Blending

BUY & SAVE
$16.99
Prina 50 Pack Drawing Set Sketch Kit, Sketching Supplies with 3-Color Sketchbook, Graphite, and Charcoal Pencils, Pro Art Drawing Kit for Artists Adults Teens Beginner Kid, Ideal for Shading, Blending
2 CZYY Acrylic Game Mat 1" Square Grid Overlay Set of 2, 6"x8" Battle Map Board Clear & Durable - Great for Dungeons and Dragons, Pathfinder and Other Tabletop RPG

CZYY Acrylic Game Mat 1" Square Grid Overlay Set of 2, 6"x8" Battle Map Board Clear & Durable - Great for Dungeons and Dragons, Pathfinder and Other Tabletop RPG

BUY & SAVE
$14.99
CZYY Acrylic Game Mat 1" Square Grid Overlay Set of 2, 6"x8" Battle Map Board Clear & Durable - Great for Dungeons and Dragons, Pathfinder and Other Tabletop RPG
3 ipxead 11 Piece Geometric Drawing Template Measuring Ruler, Transparent Green Plastic Ruler with Portable Plastic Bag for, for Studying, Designing and Building

ipxead 11 Piece Geometric Drawing Template Measuring Ruler, Transparent Green Plastic Ruler with Portable Plastic Bag for, for Studying, Designing and Building

BUY & SAVE
$11.99
ipxead 11 Piece Geometric Drawing Template Measuring Ruler, Transparent Green Plastic Ruler with Portable Plastic Bag for, for Studying, Designing and Building
4 HIFORNY 126 PCS Drawing Kit Sketching Pencil Set,Sketch Pencils Art Supplies with 3-Color Sketchbook,Graphite,Charcoal,Blending Tools,Drawing Pencils for Adults Artists in Zipper Case

HIFORNY 126 PCS Drawing Kit Sketching Pencil Set,Sketch Pencils Art Supplies with 3-Color Sketchbook,Graphite,Charcoal,Blending Tools,Drawing Pencils for Adults Artists in Zipper Case

BUY & SAVE
$49.99
HIFORNY 126 PCS Drawing Kit Sketching Pencil Set,Sketch Pencils Art Supplies with 3-Color Sketchbook,Graphite,Charcoal,Blending Tools,Drawing Pencils for Adults Artists in Zipper Case
5 Bright Creations Drafting Vellum Paper Sheets - 24 x 18 in, Translucent White, 20-Pack - Architectural Vellum Paper Overlays & Translucent Drafting Sheets with Engineer Title Block

Bright Creations Drafting Vellum Paper Sheets - 24 x 18 in, Translucent White, 20-Pack - Architectural Vellum Paper Overlays & Translucent Drafting Sheets with Engineer Title Block

BUY & SAVE
$20.50
Bright Creations Drafting Vellum Paper Sheets - 24 x 18 in, Translucent White, 20-Pack - Architectural Vellum Paper Overlays & Translucent Drafting Sheets with Engineer Title Block
6 DoodleDazzles Shimmer Markers Set, Double Line Outliner Marker, Metallic Pens, Gifts for Girls, Boys, Kids, Women, etc., School Supplies Great For Drawing, Craft, & more, 12 ct

DoodleDazzles Shimmer Markers Set, Double Line Outliner Marker, Metallic Pens, Gifts for Girls, Boys, Kids, Women, etc., School Supplies Great For Drawing, Craft, & more, 12 ct

BUY & SAVE
$24.95
DoodleDazzles Shimmer Markers Set, Double Line Outliner Marker, Metallic Pens, Gifts for Girls, Boys, Kids, Women, etc., School Supplies Great For Drawing, Craft, & more, 12 ct
7 AGPTEK 48 PCS Sketch Drawing Tools, 20 Blending Stumps Set and 2 Sandpaper Pencil Sharpeners, Pencil Extension, Erasers,20 sponge replacement heads & 1 Felt Bag for Student Sketch Drawing Accessories

AGPTEK 48 PCS Sketch Drawing Tools, 20 Blending Stumps Set and 2 Sandpaper Pencil Sharpeners, Pencil Extension, Erasers,20 sponge replacement heads & 1 Felt Bag for Student Sketch Drawing Accessories

BUY & SAVE
$12.99
AGPTEK 48 PCS Sketch Drawing Tools, 20 Blending Stumps Set and 2 Sandpaper Pencil Sharpeners, Pencil Extension, Erasers,20 sponge replacement heads & 1 Felt Bag for Student Sketch Drawing Accessories
8 AGPTEK 22 PCS Sketch Drawing Tools, 16 Blending Stumps Set with 2 Sandpaper Pencil Sharpeners, 1 Pencil Extension Tool, 2 Erasers & 1 Felt Bag for Student Sketch Drawing Accessories

AGPTEK 22 PCS Sketch Drawing Tools, 16 Blending Stumps Set with 2 Sandpaper Pencil Sharpeners, 1 Pencil Extension Tool, 2 Erasers & 1 Felt Bag for Student Sketch Drawing Accessories

BUY & SAVE
$10.99
AGPTEK 22 PCS Sketch Drawing Tools, 16 Blending Stumps Set with 2 Sandpaper Pencil Sharpeners, 1 Pencil Extension Tool, 2 Erasers & 1 Felt Bag for Student Sketch Drawing Accessories
9 Castle Art Supplies 40 Piece Premium Drawing and Sketching Set with Tutorial | for Artists, Professionals or Beginners | Pencils, Charcoal, Graphite and More | in Neat Carry-Anywhere Zipper Case

Castle Art Supplies 40 Piece Premium Drawing and Sketching Set with Tutorial | for Artists, Professionals or Beginners | Pencils, Charcoal, Graphite and More | in Neat Carry-Anywhere Zipper Case

BUY & SAVE
$31.35 $39.89
Save 21%
Castle Art Supplies 40 Piece Premium Drawing and Sketching Set with Tutorial | for Artists, Professionals or Beginners | Pencils, Charcoal, Graphite and More | in Neat Carry-Anywhere Zipper Case
10 Proportional Divider Artist Drawing Tool for Artists by Pixiss Professional Compass Caliper Scale Divider Drawing Supplies, Drafting Tools, Projector or Camera Lucida Alternative

Proportional Divider Artist Drawing Tool for Artists by Pixiss Professional Compass Caliper Scale Divider Drawing Supplies, Drafting Tools, Projector or Camera Lucida Alternative

BUY & SAVE
$9.99
Proportional Divider Artist Drawing Tool for Artists by Pixiss Professional Compass Caliper Scale Divider Drawing Supplies, Drafting Tools, Projector or Camera Lucida Alternative
+
ONE MORE?

To draw rectangles over an ImageView in Kotlin, you can follow these steps:

  1. Get a reference to the ImageView in your activity or fragment.
  2. Create a custom class by extending the ImageView class.
  3. Override the onDraw method of the custom class.
  4. Use a Paint object to define the Rectangle shape and its properties, such as color and stroke width.
  5. In the onDraw method, use the Canvas object to draw the rectangle by calling drawRect method and passing the rectangle dimensions and the Paint object.
  6. Add the custom class to your XML layout file, instead of the regular ImageView.
  7. Set the image to be displayed in the custom ImageView using the setImageDrawable or setImageResource method.

Here is an example of how your custom ImageView class may look:

import android.content.Context import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect import android.util.AttributeSet import androidx.appcompat.widget.AppCompatImageView

class RectangleImageView(context: Context, attrs: AttributeSet) : AppCompatImageView(context, attrs) { private val rectanglePaint = Paint().apply { color = resources.getColor(R.color.rectangle_color) // Set the color of the rectangle strokeWidth = resources.getDimension(R.dimen.rectangle_stroke_width) // Set the stroke width of the rectangle style = Paint.Style.STROKE // Set the rectangle style to stroke }

override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    
    // Get the dimensions of the ImageView
    val viewWidth: Int = width
    val viewHeight: Int = height

    // Create a rectangle with desired dimensions
    val rectangle = Rect(100, 100, viewWidth - 100, viewHeight - 100)

    // Draw the rectangle on the canvas
    canvas.drawRect(rectangle, rectanglePaint)
}

}

In the XML layout file, replace the ImageView with your custom RectangleImageView:

<com.example.yourpackage.RectangleImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/your_image" />

Make sure to replace yourpackage with your actual package name, and your_image with the image you want to display in the ImageView.

What is the purpose of drawing rectangles over an ImageView in Kotlin?

The purpose of drawing rectangles over an ImageView in Kotlin could be to highlight specific areas or elements within the image, to implement image annotations or to create custom overlays on top of the image. This technique is often used in image processing, computer vision, or image recognition applications to indicate points of interest, regions of focus, or areas for further analysis.

What is the syntax for drawing rectangles over an ImageView in Kotlin?

To draw rectangles over an ImageView in Kotlin, you can create a custom View class that extends ImageView and override its onDraw() method to draw rectangles using a Paint object. Here's an example:

class RectangleOverlayImageView(context: Context, attrs: AttributeSet?) : ImageView(context, attrs) {

private val rectanglePaint = Paint().apply {
    color = Color.RED
    style = Paint.Style.STROKE
    strokeWidth = 5f
}

private val rectangleRect = Rect()

private val rectangleCoordinates = mutableListOf<Pair<Float, Float>>()

fun addRectangle(left: Float, top: Float, right: Float, bottom: Float) {
    rectangleCoordinates.add(Pair(left, top))
    rectangleCoordinates.add(Pair(right, bottom))
    invalidate()
}

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)

    canvas?.apply {
        for (i in rectangleCoordinates.indices step 2) {
            val leftTop = rectangleCoordinates\[i\]
            val rightBottom = rectangleCoordinates\[i + 1\]
            rectangleRect.set(leftTop.first.toInt(), leftTop.second.toInt(), 
                              rightBottom.first.toInt(), rightBottom.second.toInt())
            drawRect(rectangleRect, rectanglePaint)
        }
    }
}

}

To use the above custom view, add it in your XML layout file like this:

<com.yourpackage.RectangleOverlayImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/your_image" />

Then, in your Kotlin code, you can find the custom view by its id and add rectangles using the addRectangle() method:

val imageView: RectangleOverlayImageView = findViewById(R.id.imageView) imageView.addRectangle(100f, 100f, 300f, 300f)

This will draw a red rectangle from (100, 100) to (300, 300) over the ImageView. You can add multiple rectangles by calling the addRectangle() method multiple times.

What are the alternative techniques for achieving rectangle overlays on an ImageView in Kotlin?

There are several alternative techniques you can use to achieve rectangle overlays on an ImageView in Kotlin:

  1. Using an Overlay View: You can add an overlay view on top of the ImageView and customize its dimensions and appearance. This can be done by creating a custom view that extends ViewGroup or by using a FrameLayout as the parent container for both the ImageView and overlay view. You can then draw a rectangle on the overlay view using the onDraw method or by using a ShapeDrawable.
  2. Custom Drawable: You can create a custom Drawable class that extends from ShapeDrawable or Drawable class. Inside the custom Drawable, you can override the draw method to draw a rectangle on the ImageView canvas. This custom Drawable can then be set as the background of the ImageView.
  3. Using Paint: You can use the Paint class to draw a rectangle on the ImageView canvas. You can override the onDraw method of the ImageView to draw the rectangle using a Paint object with a set color, stroke width, and style. This method allows you to have more control over the rectangle's appearance and position.
  4. Custom ImageView: You can create a custom ImageView class and override the onDraw method to draw a rectangle directly on the canvas. Within the onDraw method, you can use methods like drawRect or drawRoundRect from the canvas object to draw the desired shape.

Here's an example of a custom ImageView implementation drawing a rectangle overlay:

class RectangleImageView(context: Context, attrs: AttributeSet?) : AppCompatImageView(context, attrs) {

private val paint = Paint().apply {
    color = Color.RED
    style = Paint.Style.STROKE
    strokeWidth = 5f
}

override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    canvas?.drawRect(100f, 100f, 500f, 500f, paint)
}

}

In this example, a red rectangle is drawn on the ImageView canvas between the coordinates (100f, 100f) and (500f, 500f). You can adjust the coordinates and appearance according to your requirements.