Trying to make a canvas that works using MotionEvent.ACTION_MOVE. However after the thread is started horizontal lines draw correctly, but none of the other lines work or are sporadic. As soon as I release the event, the lines clear themselves.
[Edit]
It was because the canvas was nested in a ScrollView.
The only thing I can think of that "could" cause that is that you are placing the SurfaceView in a layout that's trying to scroll.
Please post your layout code for a better answer.
Related
My requirement is similar (not the same, of course) to the GuitarTuna app. I want to draw on an infinite vertical ScrollView as follows:
A trail emanating from the bottom of the pin on the top of the screen can be seen in the image above. It keeps scrolling with the ScrollView and disappears (by scrolling out of the screen) if the drawing is stopped.
What is the best way to create a similar kind of drawing interface?
EDITED
Hi!
Finally, i think, i understand what you need.
I really don't know if is possible to achieve by this using the example that you mention.
I would try the option of using a SurfaceView.
Here's what i have until now:
An interesting answer about how to draw inside scrollView:
How to draw on canvas greater than screen size and be able to scroll / pan horizontally and vertically in android?
How to draw in a SurfaceView:
example 1
example 2
Complete example of draw using TouchListener in a SurfaceView
How to draw to Canvas from SurfaceView?
Good answers that explains how a SurfaceView works :
Android: How to draw on a Surfaceview which already is displaying a Camara Preview
View and SurfaceView, which one is better?
Ideas on how to scroll a surfaceView?
Android SurfaceView scrolling
I wish to have more time to help you, and change my answer with some code.
Hope it helps!
I´m new in android development. I want to create a game where the user needs to quickly drag and drop balls with the finger in the screen to specific locations in order to win.
I don´t know if i can do this with simple ImageViews or i need to use canvas or other thing to draw in the screen.
I have tried to drag and drop an ImageView on top of another ImageView but it does not seem to work very well and i'm hesitating if i need to do it drawing figures instead of using the ImageViews or to do something else.
Please advice. Any help is apreciated. Thank you.
ImageViews are Views designed to hold image drawables, which isn't really needed in your case - you can use a simple View with a color background or implemended onDraw method, you can use an ImageView just as well.
Asfor the drag and drop, what you need is any View with implemented onTouch method so that the view changes its position on screen with ACTION_MOVE touch events.
If you're interested, i have implemented a simple frame layout with drag and drop functionality. My layout requires long-click to start drag, but it can be easily modified so it won't. Check it out at my github page. You can clone the project and run demo activity to see how it works, and start from there.
I'm creating a custom TextView by using TextPaint in the onDraw() method.
however this is causing severe problems for my app.
performance wise I mean.
The onDraw() method gets called over and over again .
but i just want it to draw once :(
i tried using setDrawingCacheEnabled(true); but no effect there.
There is also a viewflipper with textviews in the main layout rotating automaticly,
could this be causing everything to redraw ?
can anyone give me some pointers here ?
As well as enabling the drawing cache, try creating a new bitmap instance in the inDraw and pass that to the Canvas to draw to. In the next call to onDraw, simply draw that bitmap if it is not null. I'm doing this myself and it works a charm to improve performance, though unable to copy the code right now (out using phone).
As for why it is being consistently redrawn, are there any moving overlapping graphics causing it to be invalidated?
In Android 2.2, I want to display a few sprites and clickable animations over an existing view. I just discovered that SurfaceView cannot be transparent
I tried overriding dispatchDraw() of a LinearLayout but that doesn't seem to be callable via a Runnable Thread.
I also tried to extend Canvas directly, but that quickly turned into a mess when trying to place it in my XML layout.
Do I have to use GLSurfaceView to have a drawing view whose background is transparent?
Optimally, I'd like to just have a Canvas on which I can draw Bitmap at various coordinates, instead of dealing with the details of GL (unless GL is the only way I can do transparency).
Silly Rabbit. Just use Views and move them around wherever you want within a FrameLayout. SurfaceView is not designed for what you're trying to do.
I'm going to try Switching from Canvas to OpenGL and toss in parts of TranslucentGLSurfaceView.
I'm still open to selecting an answer that doesn't require OpenGL, just for the sake of it solving my original question.
I currently have an app with a regular layout of buttons and widgets. On top of this I'd like to draw some animated sparks and particles and whatnot going on in response to events, so I've got it in a FrameLayout with another View on top to draw the animations. The problem is I can't work out a way of getting smooth movement out of it. I've tried a few options:
SurfaceView: because of the way it takes over the screen, you can't see anything behind a SurfaceView so the background is fully black.
Override View.onDraw and call invalidate(): this almost works, but invalidate isn't a very reliable way of getting a redraw to happen soon, so the motion is very jerky.
Animation framework: Testing with TranslateAnimation, it seems a bit smoother than using onDraw(), but animations are designed to run for a specific duration and I want to draw indefinitely.
Anybody know any tricks to make one of these work properly, or something completely different?