As is known, drawCircle(x, y, radius, paint); takes in the canvas coordinates, which may be different from the screen coordinates.
So if I just want to draw something on one particular point on the screen, how would I do that with that method?
I am asking this because the canvas may be moved or even zoomed, but I do NOT wish my circle to move. I want it to stay at that particular point on screen.
Here is the description of drawCircle() method.
It seems that there are no direct converting methods.
We have to convert it manually by the help of maths.
We can process the coordinates according to the properties, like translation or scaling or even rotation.
Related
I have a graphing application that has an image displayed on screen. The image is dependent on internal variables for minimum and maximum X and Y coordinates.
For example the default coordinates may be from (-5,-5) to (+5,+5). Using these coordinates I draw the contents of the image. Think of a complex graph/image that can be panned and zoomed.
In Android I use an onTouchListener, postScale, postTranslate and setImageMatrix. This allows the user to drag and pinch zoom the onscreen image. This part works as expected. The user can pinch zoom and the onscreen image is zoomed in and gets blurry. The problem comes when they release their finger(s) and I need to regenerate the imageview contents. I need to know how the internal minx, maxx, miny and maxy coordinates changed in relation to the android coordinates.
How can I translate scale my coordinate system by the pan/zoom amounts the user pans and zooms? I have setup all sorts of bad code to try and push/pull/scale my coordinate system based on the imageview matrix transforms, but nothing works reliably. Is there a simple set of math formulas to help me do what I want?
I hope this isn't too vague.
In my android app, I want to draw circles and then have to find their intersection points. And then i need to draw the lines between the intersection points which i got. I know how to implement this logic but dont know where to draw like graph.
I tried to draw this on mapview using Geopoints, considered lattitude as "x" axis and longitude as "y"axis. Its not good enough as I expected.(Sometimes lines disappear when zooming).
Which feature of Android facilitate this? Guide me to implement this.
Thanks in advance...
You want a custom view. Custom views allow you to draw to a Canvas. Canvas has functions like drawLine and drawCircle. It takes x,y coordinates, with 0,0 being the upper left corner and x and y growing bigger as you go right and down. If you don't want 0,0 to be the upper left, you can always use some basic algebra to translate it wherever you want.
I'm struggling with something I would expect to be straight forward with libgdx.
In short this is a "finger paint" app where I want to draw a path of a certain width where the user touches the screen.
I've earlier done this by using a plain Android android.view.View. I had a android.graphics.Path in which I stored the coordinates of the user's current touch. In the onDraw() method of the view I drew the path to the android.graphics.Canvas. Whenever the user released a finger I drew the path to an offline canvas/android.graphics.Bitmap which also was drawn in the onDraw() method. Plain and simple.
How can that be done using libgdx?
I have tried using a com.badlogic.gdx.graphics.Pixmap that I can draw a line to whenever the user moves a finger. This works well except the fact that I'm unable to control the witdh of the line using Gdx.gl.glLineWidth(). I know I can draw a rectangle instead of a line to set the width, but Pixmap doesn't seem to have any means of rotating, so I don't see how this can be done.
I can use a com.badlogic.gdx.graphics.glutils.ShapeRenderer for drawing lines (or rectangles) in com.badlogic.gdx.Screen.render(). As far as I can see I then need to store every single touch point of the current touch, and then draw all lines on render. Whenever the user relases a finger I guess I can store the screen as-is with something like com.badlogic.gdx.utils.ScreenUtils.getFrameBufferPixmap(). Hopefully there is a easier way to achieve what I want.
I ended up drawing circles on a pixmap where the line should be:
One circle on touchDown
Several circles from last touch point to next touch point reported to touchDragged
I'm not very happy with this solution, but it kinda works.
Maybe you can calculate line dot coordinates manually and use triangles to draw them on pixmap? Like use 2 triangles to form (rotated) box?
I am using FingerPaint example available in ApiDemos for drawing my finger movement on screen.
Now instead of line, I am adding circles in my path object and this path with circle will be drawn when onDraw method is called.
Now my issue is when I slowly move my finger it draws circles at the points given by onTouchEvent properly but when I move my finger with some speed, unlike line, it draws only few circles.
After debugging, I found that while moving finger rapidly, some of the touch events are dropped by the view and on that points circles are not drawn.
Can anyone give some pointers on this. Why this is happening?
Two things I can think of without seeing code.
First, are you getting the historical points recorded between touchEvents and using them also? If not, that would cause some serious jerkiness like you are talking about.
If you are, and it's just not picking them up, the best you'll be able to do might be to cheat it some. Check the distance between the current and last circle drawn. If it exceeds the circle's diameter too much, draw a line with the same thickness as the circle, with a circle on both ends.
I am trying to rotate a canvas with canvas.rotate and move an object on it at the same time. The problem is that with the rotation, the coordinate system of the canvas rotates as well, so I get cases when my object is supposed to be moving along the y axis, but the y axis is rotated on place of the x axis. It is a mess. Is there a way to go around this?
It's using matrix math; if you do things in the opposite order (translate then rotate, or vice versa), you'll get the opposite effect.
Also, use SetMatrix(null) to clear the matrix to the identity between operations; not sure if that's the kind of mess you're having trouble with.