Development issue/problem:

You can find an example compass.java here.
This is Api.

How can I solve this problem?

Solution 1:

Translation – Essentially doing what’s been written. Just move the cloth with x, y. If you want to draw two objects and one is just a translation of the other, for example x2 = x1 + 50 for each point . You don’t have to do all the calculations for the second object, you can just move the canvas and redraw the same object. I hope this example will help you.

Solution 2:

Even when I first answered this question a few years ago, I didn’t really understand how the canvas was transformed (e.g. by translation, rotation, etc.). I always thought the translation changed what you drew. In fact, the translation moves the entire coordinate system. This has the desired effect of moving the object you are drawing.

On your screen, it looks like you’re moving the image:

What does canvas.translate do? –

In fact, the coordinate system is moved to a new position on the web:

What does canvas.translate do? –

First I draw the tree at (0.0). Then I move the origin of the coordinate system to another location on the web. Then I mark the tree again (0.0). So my code for the drawing doesn’t change at all. Only the coordinate system changes.

Normally (0.0) is located in the top left corner. Running Canvas.translate moves it to another part of your view.

Backup and recovery of coordinate system

You can perform save() and restore() to return to the original coordinate system.

// Draw the tree for the first time
canvas.drawBitmap (tree, 0, 0, mPaint) ;

// draw the tree a second time
canvas.save();
canvas.translate(dx, dy); // dx = change to x, dy = change to y
canvas.drawBitmap(tree, 0, 0, mPaint); // re-sign it as if it was on (0,0)
canvas.restore(); // cancel the translation

When you restore the drawing, it is already on the canvas. The restoration doesn’t change that. It just returns the coordinate system back to where it was when you stored it.

Why translate

Note that the same effect can be obtained by changing the x,y coordinates of the drawing method:

// Draw the tree for the first time
canvas.drawBitmap (tree, x, y, mPaint) ;

// Update coordinates x,y
x += dx ;
y += dy ;

// draw the tree for the second time
canvas.drawBitmap (tree, x, y, mPaint) ;

This can be more intuitive from a mathematical point of view. However, if you translate, rotate and scale your image, it is often much easier to maintain the logic of the drawing and simply transform the canvas. Recalculating x and y for each print run can be very costly.

Solution 3:

Think of it as a print head.

The easiest way to explain this is to imagine the print head of an inkjet or 2D printer.

Translate the print head essentially along the X and Y axes by the number of pixels you specify.

The resulting position becomes the new start position (0.0).

Now that we’ve understood that, let’s make a simple face by doing the following:

Origin:

x

X stands for (approximately) the location of the source (or print head).

Draw a rectangle for the left eye:

canvas.drawRect(10, 10, 10, paint) ;

x__
|__|

Pay attention: The origin hasn’t changed, it’s still in the top left corner of this rectangle.

Move the beginning of 20 points to the right:

canvas.translate(20, 0)

__ x
||||

Draw the right eye with exactly the same rectangular command:

canvas.drawRect(10, 10, 10, paint) ;

__ x__
|__|.

Return the origin to the original X-axis position and move it down the Y-axis:

canvas.translate(-20, 20) // Positive numbers for the second parameter are on the y-axis.

__ __
|__| |__|

x

And draw a mouth to end it:

canvas.drawLine( 0, 0, 30, 0, paint );
__ __
|__| |__|.

x___________

Now all we have to do is slide down the first 20 points to reveal our masterpiece:

canvas.translate(0, 20)
__ __
|__| |___|

___________

x

Not ideal for scaling, because you can’t do much with a monospace font.

Solution 4:

it changes the position of your canvas (except for the scale) either x or y
, if we translate and scale, in general terminology it is a transformation

Good luck!

canvas translate rotate,canvas translate to spanish,canvas translate google,canvas rotate android,android canvas,translating the canvas allows you to draw in 3d,canvas transform-origin,android canvas drawbitmap,canvas drawrect,android canvas draw rectangle,android canvas scale,canvas.restore android,canvas scale and translate android

You May Also Like

Know All The Usefull iOS 13 Tips And Tricks

There are a few useful tips and tricks for iOS 13 that…

A Simple Yet Powerful WordPress Alternative for Content Creators

As with the alternatives to WordPress, it is undeniable that blogs with…

What all can a Zoom host see when you screen share?

In a Zoom meeting, the host and other participants can easily share…

Download Google Camera 7.3 for Samsung Galaxy A42 5G [Best GCam]

The Samsung Galaxy A42 5G is one of the most affordable 5G…