40727

Drawing bunch of rotated rectangles on android canvas

Question:

I have a task to draw many rectangles on canvas, but all of them have a rotation angle by which they have to be rotated on canvas. Many of suggestions I ran into while searching for solution of this problem indicated the way to draw a rectangle and rotate the canvas (Canvas.rotate(angle)), but it rotates all canvas and is only possible with one rectangle. What could be the best way to draw many of rotated rectangles on canvas? I want to draw rectangles (single color, with Paint), but not bitmaps, due to time efficiency and memory.

The primary way I would do currently is creating a load of canvases and drawing one rectangle on each of them and rotating canvases considering the angle of rectangles. I think that it is not a smart way due to many canvases and for each of them I should create a separate SurfaceHolder and it is a mess...

Note that for each rectangle I have coordinates of all of its 4 corners, its width, height, center, angle.

Answer1:

You can rotate the canvas for drawing each rectangle, and then restore the original orientation after. Then set the new rotation for the next rectangle, draw, store, and repeat.

Approximately this:

//Save and rotate canvas canvas.save(); canvas.rotate(angle, pivotX, pivotY); canvas.drawRect(...); //restore canvas canvas.restore(); // rotate and draw the second rectangle canvas.rotate(angle, pivotX, pivotY); canvas.drawRect(...); canvas.restore(); // repeat as necessary

where 'angle' is different for each rectangle.

Recommend

  • Karate - XML - Printing Child node
  • How to block a specific user agent in Apache
  • Wait/Pause/Sleep in jQuery Each Loop between Iterations
  • returning a value from a provision script to vagrant
  • How do I make an “empty” anonymous function in MATLAB?
  • Difference between return View() and return View(new Model())
  • function overloading in swift [duplicate]
  • Should I define variables in short blocks/functions of JavaScript? [closed]
  • How to generate a SHA256 hash of 32 bytes using nodejs (crypto) in order to avoid bad key size error
  • How to enrich a TraversableOnce with my own generic map?
  • FormsAuthentication.CookieDomain property
  • how does heapq.merge in python work
  • Simulating argparse command line arguments input while debugging
  • How to customize marker colors and shapes in scatter plot?
  • Is there a way to remove a script from a doc (using the new doc embedded script)
  • android 3G signal strength
  • UIImage to UIColor array of pixel colors
  • Magento Layered Navigation block. Move to center
  • Logout user after Woocommerce Checkout
  • Enable CORS on Tomcat 8.0.30
  • using JQuery autocomplete from database with PHP (CodeIgniter)
  • Jenkins - could not read Username for 'https://github.com': No such device or address
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • How to move to lines with the same indentation in Visual Studio Code
  • How do I set the logging properties in a spring java configuration?
  • Unable to run testNG tests from maven
  • Modifying native query cannot have named parameter bindings?
  • Android studio import problems. (Apktool)
  • Use AutoIt with java applications
  • How to select multiple items from a List view - JavaFX 8
  • Magento-Change Attribute of All Products
  • Find angle of point on circle
  • Zoho API: Get the user that is making the request
  • Another “Cannot make static reference…” Question
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • multiple button click in asp.net MVC 3
  • Creating random wired topology for given arbitrary number of nodes on NS2
  • How to check if object is null in Java?