I'd like to include a simple draw feature on my app. A friend told me I can't use canvas because I need to control the frame buffer and not redraw the entire screen every for every frame. He suggested I find a openGL 2d engine. I've searched around and I've only found Rokon which doesn't have any documentation and the examples aren't quite what I need.
Any suggestions?
I don't really understand what the problem is?
If you simply wish to redraw some portion of the canvas you can use invalidate(rect).
http://developer.android.com/reference/android/view/View.html#invalidate()
Just create a custom veiw by extending the view class. in this custome view override the onDraw method. Android itself takes care of the pixels that have been changed and calling invalidate only refreshes the pixels that have been marked dirty
Related
I need update a small portion of a custom view in order to display a small animation. The rest portion of the view has only static image. The most straightforward would be to obtain the canvas of the view and update only that particular portion directly. But I can't really find anyway to get the view's canvas object outside of the view::onDraw method.
The only alternative I know is this: call view::invalidate() with a specified rectangle to minimize the drawing flicker. I have the code to update the entire view within onDraw. So the best thing to do is to detect the clipping rect and only run the code to update the specified area, in order to minimize CPU usage as well?
I guess I will try to answer this question myself to the best my knowledge so far.
There is no direct access to the canvas outside of the onDraw method.
Although we can detect the clipping rect with the function Canvas.getClipBounds(), the getClipBounds function always return the entire view area if GPU is enabled. When GPU is not used, getClipBounds() returns the actual dirty area. Since there is a GPU in most phones, it makes the function getClipBounds pretty much useless.
I have created a custom view to be shown in the action bar. It mostly works except sometimes on start I see a mirror copy of whatever I draw. The copy overlaps the original one but a few pixels away.
My onDraw() override is quite simple. All it does is draws an image that is centered in the canvas.
I am a bit confused. Am I expected to clear the canvas first in onDraw() method? Thank you in advance for your help.
It is confusing, but you'll notice in the custom view samples (that come with the sdk), the onDraw() method first calls canvas.drawColor() before anything else.
I assume it's not done automatically because it would be wasteful in the case where what you were drawing filled the entire view anyway. I just wish it was more clear that it is necessary in most cases.
Here is what I found out that others may find useful. If you set a background using setBackground(), the framework will actually draw the background before calling onDraw(). This background is drawn in the center. I too was drawing the background in onDraw() but I was drawing it from top-left. Hence, I was seeing the ghost image.
As Krylez mentioned, you may wish to call drawColor() as your first call in onDraw().
What I did was overrode setBackground() and its variations, and stored the background bitmap in a local variable.
Regards,
Peter
I want to draw a bitmap to the canvas.
However, I am an inexperienced programmer and don't know how to make the canvas the main screen.
Basically, I want to draw a bitmap to the main activity using a canvas. I know how to draw bitmaps to canvases but it doesn't show up anywhere that I can see (with only one activity). I want the bitmap to show up on the main activity. Any tips?
Sorry if the wording is confusing, thanks for any help.
see these links. might think they may be helpful.
https://www.linux.com/learn/tutorials/707993-how-to-draw-2d-object-in-android-with-a-canvas
http://www.linux.com/learn/tutorials/703911-2d-drawing-with-android-motion-sensors
you can try searching online..
some example
http://www.edu4java.com/en/androidgame/androidgame2.html
or some game engine which will help you to develop game with proper memory management.
https://stackoverflow.com/questions/17163446/what-is-the-best-2d-game-engine-for-android
What you are trying to do is create your own CustomView. When you are putting a button or anything in your layout file that is actually a view. Android does allow you to create your own view to manually handle things on the screen. Games are usually implemented that way. They use customview by extending SurfaceView/GLSurfaceView.
As for the solution of your problem:
create a custom view by extending the View.
add all the constructor from superclass.
override onDraw method
onDraw method will pass you a canvas for you to draw
draw whatever you like(Bitmap, line ,....) using canvas
In your layout file include the customview you just created.
<my.packagename.MyCustomView android:width .... > </my.packagename.MyCustomView>
Hope this helps.
Also you will find lots good resources on how you can create your own customview.
I coded an android application which is measuring heart rate from camera input. I needed to desing an ECG animation to the application. I know that ECG isn't related. But I'll try to show the sign according to heart rate. Which method do I need to use? Drawing animation into a View object from layout or Drawing graphics directly to a Canvas. And if you give some clues I'll be very appreciated.
To do real time graphing of ECG data...which is what I am coding right now, you need to create a custom view. Then in the onDraw method you use your canvas to draw a bitmap. Then inside the custom I also implement a Runnable that paints the line using drawline, and then invalidates(). I'd show you code but its proprietary since everything I found did not measure up to the speeds I had to graph.
It depends how real-time you need your data. If a lower framerate is sufficient, then I would recommended subclassing the View class and overriding the onDraw method. From there you can draw directly onto the canvas. Just make sure to call invalidate on the view after your data changes to make sure onDraw gets called.
If a faster framerate is required, then you probably want to use a SurfaceView. Google has a Lunar Lander example that does a good job of that. But note some bugs have been found in the example, so use it as a guide not as law.
Lunar Lander:
http://developer.android.com/resources/samples/LunarLander/index.html
I have another question related to this disk: http://dl.dropbox.com/u/8051037/disk_ab1.png
I have created a custom view, and set everything up to rotate it, but I don't know how to set the rotation of the view when the disk is spinning. I think I could've done this with an Animation object and a TimeInterpolater, but I couldn't figure out how to use either of those, so I created my own little algorithm for doing it. The code I have can be found at:https://github.com/c0dege3k/StarkOMFGB/blob/master/Mods/LauncherDisk.java.
Thanks for ANY help, this and my other question for this disk have been giving me serious problems.
The easiest way will be to rotate the canvas than to draw the image and restore the canvas. For more info take a look here Rotating a view in Android