Drawing lags in Android drawing application with canvas - android

I am building an drawing application in Android using canvas. I'm directly painting on the canvas without using the offline bitmap/canvas. Everything works fine on a 512M memory device but on slower devices, the application lags after a few strokes. The application allows user to put some cliparts, background and flood fill, too.
For all this different objects I am using a ArrayList with a bean class to store each paint object, its style and its bitmap.
In onDraw(), each time I am iterating over this ArrayList and draw everything that is stored in that ArrayList. I am providing undo and redo so cannot use drawingCache.
In short, I need to improve overall performance of the application. How can I achieve that? Is the application structure proper or anything that I should change? I can post some code if needed.
Thanks in advance.

Related

peer to peer collaborative drawing

I am developing a peer to peer collaborative drawing app on android using alljoyn framework like chalkboard .
I am able to implement collaborative chat among peers. Now i want to implement canvas sharing where in a single canvas everyone will be able to draw in real time.
How can i start with canvas, what would be its data structure,is there any specific image object i need to handle,do i need to use json,do i have to store the pixel values in a 2D array.
I need only a black & white screen with white background and black as drawing part.
I just want to know the approach behind it. Any reference will be helpful.
thanks...
Canvas is really a bitmap.
You add/change pixels on the bitmap using drawing commands.
To do collaborative drawing, you wouldn't share the pixel values between all users with each change.
That would create bottlenecks in serializing, transport and deserializing. It would be too slow to work.
Instead, share the latest drawing commands between all users with each change.
If user#1 draws a line from [20,20] to [100,100], just serialize that command that drew the line and share that with all users.
Perhaps the serialization might look like this: "L 20,20 100,100".
If you want an efficient serialization structure, take a look at the way SVG does it's path data--very efficient for transportation to many users.
All other users would listen for incoming commands. Upon receipt, they would deserialize user#1's line and have it automatically drawn on their own canvas.

Android drawing app. Draw, Save, Edit

I am new to Android and trying to develop an app that allow user to draw, save them which later on is able to reopen to edit and save back into the storage.
I have got the solution of drawing and saving part. However I am stuck with no idea how I can enable user to reopen the file for edit.
Will be grateful if someone could shed some light and provide me with clues or example how can this be done.
Thanks.
It depends entirely on how you draw and save the image. If you are saving it in a bitmap (png or jpeg) load it again and do other drawing operations with Canvas. You can do the drawings using a SufaceView

Best Way to Animate images frame by frame in Android

I have 3 sets of 50 images and I have to create the animations for each set of images in Android application. I am able to create a simple application which animate first set of 50 images using the below method,
Added Animation-list xml in drawable folder and called it using frameAnimation.start().
This method didn't work until I kept the following "android:largeHeap="true" in manifest file.
I am not sure whether this is the good way to animate the images (if we have more number of images and each image of more size like 60 KB. Image is JPG format) or not
I browsed and I found that, if we are able to clear the memory and if we are able to maintain less number of images in memory, then our application will work very fine. So I want to know how to clear the memory?
Please let me know do I need to follow different method to animate the images other than I explained above, so that I can clear the memory.
Thanks for your help.
After looking into several posts and doing much research, I finally ended up modifying the images in Imageview programmatically to simulate frame animation. That way I am just maintaining three images in cache at any point of time (previous, current and next one). I hope that this will be useful to others.
It is not a good thing to process a large amount of images in a frame like manner in Android itself as it will trigger the dreaded "Out of Memory" exception.
Clearing the memory itself is not possible. The best fix for this is proper bitmap handling of the app.
I'm not sure but you might want to check on PhoneGap.
Its an HTML5 Engine that we used before to create a game.
By drawing into the canvas itself, we've recreated frame animation with it. It just requires WebDev skills though.

Using SVG files with libgdx

I'm planing to create tablet app. I would ask for some guidance.
I have pictures in SVG format like this one.
With SVG it is easy, You just change fill parameter to different color but as I understand, there is no easy/stable svg processing to use with libgdx. I still want to use svg files to create/store images for my app.
What processing path would You recommend?
Is there an easy way to convert svg paths/shapes for com.badlogic.gdx.math.bezier or polygon objects and then draw them on screen/get user input (tap) inside this shapes?
Or should I use different objects/path?
Shapes could be grouped together for example I want two windows in a house to change color at once.
The way LibGDX is written is it gives you a lower level way to do this type of rendering, but doesn't offer out of box ways to render SVG. It really depends on if you are looking for something with performance or just simply want it to draw basic shapes.
To simply render shapes you could use something like ShapeRenderer. Which gives you a very close interface to the Java2D way to draw things. Maybe to quickly draw some basic stuff this might be handy.
If you are wanting to do some more robust version of rendering you will probably want to look into using a Mesh and working with shaders for OpenGL ES. You can find examples of these in the LibGDX tests as well as searching online for examples/tutorials.
If you want to turn the SVG into a texture you will want to look at Pixmap and then you can create a Texture with it and render with a Spritebatch. You will need to write the pixels you want to color and such with the Pixmap. However, doing it this way will generate an unmanaged texture (i.e. you will have to rebuild it when a user comes back to the app after pressing back or putting the device to sleep).

Android App - Can we Reuse Images in the Run time?

We have an Android application that throws out of memory error on Android 1.6. It is working fine on 2.0 onwards. We are trying to cut down on the runtime memory usage at this point.
We were thinking of reusing images – such as the background image of each screen. Is it possible to load the background image once in the memory at the time of app launch and reuse this cached copy in each screen rather than each screen loading the same background image increasing the overall footprint in the RAM?
We understand that there many other ways and best practices around runtime memory usage. But at this point, we just wanted to know whether this “image reuse” approach is really feasible?
If yes, how can we do this? Will it at all save any memory or the Android OS will still create multiple copies of the same asset for each screen not giving any benefit from the memory perspective?
Thank you very much for your time in reading my post.
If that background image is loaded as a Drawable from a Resource, it's bitmap data is already shared between all Drawables created that way. So no, it probably wouldn't benefit you in your particular case.
Yes you can reuse images.
It is usually done for ListViews where each row contains the same image.
In this case, you can get the Bitmap once in the constructor of your adapter, and reuse it in the getView() method, so you don't have to load it for each row. This trick is presented here
In your case, you could load your background bitmap in the onCreate() method of your main activity, save it as a public static attribute and then re-use it in all your activities.
But I think it won't make that much of a difference and surcharge the code for nothing.
As you are working on tweaking your UI, I suggest you watch the 2009 Google I/O Presentation by Romain Guy, if you did not see it yet. It shows a lot of useful tricks for boosting an UI's performances, especially when it comes to Bitmap manipulation.

Categories

Resources