Question : Is there any better way to implement a function that draws line on the screen other than drawing circles on the input points ?
Details : I am trying to write a function which basically will draw a line.However the line will not be a regular colored line. This line will let user to copy pixels from one image to another trough that line , you can think It like "history brush" or "stamp" on Photoshop . My first thought was just get the points and draw circles on each point. Which is working but not smooth and have some bugs. The first problem is it is not working if user sweep his finger too fast. I thought it would solve problem if i can fill the dots between two points and then draw the line which is kind of work but this time drawing gets very slow. One other problem is that it never looks smooth.
Bresenham's line algorithm
Isn't the BitmapShader what you're looking for?
Related
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?
im new to this android things. And i have to develop an application that can help an autism to learn numbers. I have a few ideas and I've been trying to learn and implement the code. But it's failed. The question is how can i apply the motion code or sprite to draw a numbers or letter? For example like this, i wanna make the penguin move through the line and draw a number nine.
There is example from mybringback.com which is the image move to draw a rectangle. How can i implement it to draw a number? Im sorry if i asking too much, i just trying to get some ideas.
I think that you should first build an utility program, in order to create the "path vector".
What I mean by path vector is simply a vector of Points (where a point has x value, and y value). And your utility should let you draw whatever you want, with a simple pen. You should draw on surface and store points when mouse is down, and ignore points when mouse is up.
Then, in the main program, you will just have to read at the path of your number/letter.
I've tried to implement something like this for the Sugar OLPC platform, without serializing path into files : I was able to draw, and to view the animation. And I used the process I've just described you.
Hope it can help you.
P.S : I used the word mouse, but you guessed that I talk about finger ...
There are various ways to achieve animation effects. One approach that is quite versatile involves creating a custom View or SurfaceView in which you Override the onDraw method. Various tutorials can be found on this; the official Android discussion of it is here:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#on-view
Your implementation will look something like this:
// Find elapsed time since previous draw
// Compute new position of drawable/bitmap along figure
// Draw bitmap in appropriate location
// Add line to buffer containing segments of curve drawn so far
// Render all segments in curve buffer
// Take some action to call for the rendering of the next frame (this may be done in another thread)
Obviously a simplification. For a very simplistic tutorial, see here:
http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/1733/
Note that different implementations of this technique will require different levels of involvement by you; for example, if you use a SurfaceView, you are in charge of calling the onDraw method, whereas subclassing the normal View lets you leave Android in charge of redrawing (at the expense of limiting your ability to draw on a different thread). In this respect, Google remains your friend =]
I have a Line class composed of two Points of two ints each that I draw with a wrapper over Canvas.drawLine().
Easy so far.
I want to have that Line drawn slowly from one Point to the other. My best guess is to make a function that will dice up my Line into an list of Lines, starting from the first Point with each subsequent Line getting longer and longer till it reaches from one Point to the other. Then, I will have a Canvas.drawLine wrapper that will take that array of Lines, and iterate over them, drawing each one with a pause of some sort in between them, giving the appearance of the line "growing".
Is there something in the android libraries that already does this and/or would this be better solved some other way?
Edit: This is android 2.1
Android has libraries for creating animations. Look into tweened animations and the AnimationDrawable class.
Or maybe you can have one line with a fixed starting point and on each draw the end point increments . I believe this approach has better performance .
I am new to AndEngine and very happy that it's very easy and exciting thing to do. Unfortunately I am unable to draw a curved line in AndEngine.
Actually my scenario is that I have an animated sprite say Object. I want to move this object on a Line with the points given onToucing and dragging it. Now the problem is that I can't find any method to draw a line on points in a way that it does not produce corners. I want a smooth line with no corners.
Suppose I touch the object and drag it on the screen with the points of a square type region. But I don't want the corners in it. I want curves. Previously I am doing this by using quadTo(..) function of android to do this. All I want is a complete alternative to quadto function in AndEngine but with same functionality.
You have to stitch together the curved line from many many small straight lines.
Moving an object along a line is a very different task though. You'd want to have a look at CubicBezierMoveModifier and QuadraticBezierMoveModifier for those.
Maybe you can actually use the code in those modifiers to create your 'smooth' line.
I would like to draw a slash animation between 2 points that I have already randomly generated.
The animation can be as simple as the line extending to the other point over a set amount of time. I would like the animation to be a "pretty" line so I was using a bunch of images and iterating over them and not just canvas.drawLine(x, y, u, v, paint).
The main issue I am running into is the points are not always the same distance apart or same direction. Im not sure if having a set number of animation sequences would work because of those differences. What is the best way to do this?
Not sure if this has been answered, but you'll have to say how complicated your imagery is. If it's a simple slash line, then drawLine would work. You'll also have to say how you are animating the line, like Android XML or if you are using some sort of timer.
There's so many ways to do this. If it's a simple line, you can use math to increment some coordinates. Watch out for using frame counting, where you say "increment the animation for the frame". It's a quick-and-dirty way to animate, and sometimes will get the job done.
A lot of video games use time-based drawing. "If this amount of time has elapsed since the last draw, draw this much". The result looks much more natural and the same between devices with different amount of horsepower.
There's also what you are drawing on. Are you using a View or a SurfaceView? The list goes on.