In my WinRT app I need to draw about 3000 objects on a canvas, where I can translate and zoom the view. Unfortunatley, after adding about 1500 lines to my canvas my Windows 8 App always crashes. What could be the best practice to achieve this?
One solution could be rendering everything on an image (how do I do this?). But then I loose comfort of easy access and editing of every element.
Also my scale and translate is very slow. But since I also need a big overview, it makes no sense to put only the objects of the visible area in the canvas, since on minimum zoom it's still everything and zoomed it's still very laggy cause of add and remove operations.
There are a couple of different things you should employ to have a smooth UX:
Use a Quadtree, whenever you add a shape to your canvas you also put it on your Quadtree. This will be helpful when you will zoom on a portion of the image: you will know what objects are in this portion of the image; you will render them again (against using a cached/pixellated version).
To overcome the potentially lengthy drawing process you could do the following:
display the portion of the cached image overview at the right scale
use a progress indicator to let know the user that the program is working render this portion
when the faint rendering is done, blit it on the screen
A concrete example: Google Maps does that.
Related
I'm trying to build a unity project for standalone and the game is in portrait configuration. Everything works fine when I test it in the editor, but when I build it, everything UI related is messed up and by that I mean it tries to fit to the screen's aspect ratio causing many buttons and UI elements to be off-screen despite even manually trying to change the screen size by doing Screen.SetResolution(1080, 1920, false); and stopping the game from entering full-screen mode in the build settings so that it would become a portrait aspect ratio but nothing is working. The game itself is working fine which is not a part of UI or any canvas obviously. Any help would be much appreciated!
UI scaling is tricky particularly is you have different aspect ratios, and also if you have different resolutions (although they are different). The most important things from my experience are making sure you've anchored things as reasonably as you can, used layout groups for dynamic content (dropdowns, instantiated elements, etc.) and used the CanvasScaler component on your canvas.
The Canvas Scaler component can be found on your canvas object in the scene, and is the first place to start when dealing with variable screen sizes. . This mode is the best for variable screen sizes, you can set this up to be your preferred screen size and then make your UI fit that size, then automatically scale based on your ideal size.
It's hard to say what is the best way to anchor your UI elements generally speaking, but if you don't want them centred on the middle of the screen it likely isn't the default. I often find anchoring the sides of an area, or corners, is best.
Finally, depending on your platform and handled screen orientations, you can also set device orientation preferences in Player Preferences. . Exactly what you set may depend on your project, but can be an important step in making sure your UI scales correctly for you targets.
I want to animate a UIImage, or better said a UIView. I've got a Background consisting of multiple layers where each layer should be animated a little differently (lets assume this is a picture of a jungle and the second background layer is a plant). My first thoughts were to use a gif, but sadly this results in poor quality and big image sizes. The next thought was to use a "sprite sheet" image where the animation consists of 4 images which will be updated every 0.x seconds. Well this could work but i wanted to code as much as possible and use as less images as possible.
After the background is animated I want to animate a monkey running from the left to the right side. For sure the monkey itself should a animated and it should do this again after a random amount of seconds.
Which way would you implement such a thing and where would you start?
Edit
Forgot to mention that this should run with as low battery usage as possible
Hello: I am trying to create an app which will display a moving sphere. App will vary speed and direction. I've tried Adobe Flash but cannot get it smooth. Smoothness is essential in this case. So I am trying C#.
Initially, I can see that this can be implemented by:
1) Creating a PictureBox of a sphere, and using a Timer, change its coordinates. or
2) Using the this.paint function to draw a filled circle, and somehow, with a timer, erasing and redrawing it.
Can someone recommend the best path to take? I'll have a main menu where the user will chose speed/direction/how many etc... and then simply show the "game window" with the moving spheres. Any guidance would be much appreciated.
This is to be displayed on a PC only.
Thanks
-Ed
I just answered a similar question here.
NOTE: Depending on your needs, it is possible to achieve smooth animations under winforms (under certain conditions) though you are responsible for everything. wpf provides an animation framework but wpf is perhaps a milestone harder.
It probably does not matter should you pursue winforms first or WPF. You arguably could learn the basics under winforms then move over to wpf. wpf may require you to learn quite a bit before you can do anything.
Summary
Essentially what this does is to create an offscreen bitmap that we will draw into first. It is the same size as the UserControl. The control's OnPaint calls DrawOffscreen passing in the Graphics that is attached to the offscreen bitmap. Here we loop around just rendering the tiles/sky that are visible and ignoring others so as to improve performance.
Once it's all done we zap the entire offscreen bitmap to the display in one operation. This serves to eliminate:
Flicker
Tearing effects (typically associated with lateral movement)
There is a Timer that is scheduled to update the positions of all the tiles based on the time since the last update. This allows for a more realistic movement and avoids speed-ups and slow-downs under load. Tiles are moved in the OnUpdate method.
If you note in the code for Timer1OnTick I call Invalidate(Bounds); after animating everything. This does not cause an immediate paint rather Windows will queue a paint operation to be done at a later time. Consecutive pending operations will be fused into one. This means that we can be animating positions more frequently than painting during heavy load. Animation mechanic is independent of paint. That's a good thing, you don't want to be waiting for paints to occur. xna does a similar thing
Please refer to my full SO answer complete with sample code
Here are a few hints to get you going:
First you will need to come to a decision about which platform to target: WPF or Winforms.
Then you should know what to move across what; a nice Bitmap or just a circle across an empty background or a Bitmap or a Form with controls on it.
In Winforms both your approaches will work, esp. if you set a circular region see here for an example of that. (The part in the fun comment!)
And yes, a Timer is the way to animate the sphere. Btw, a Panel or even a Label can display an Bitmap just as well as a PictureBox.
For smooth movements make sure to set the Form.Doublebuffered=true, if you move across a Form. If you move across any other control (except a PictureBox or a Label) you will need to subclass it to get access to the DoubleBuffered property!
It is often also a good idea to keep the Location of a moving item in a variable as a PointF and use floats for its speed because this way you can fine grain the speed and Location changes and also the Timer Intervals!
I'd like to implement a similar screen clipping functionality to that of OneNote. Basically it can draw a translucent overlay on top of the whole screen, and also freeze the screen so users can clip a portion of it.
I've done some research around and it seems that the easiest way is to create a translucent TopMost form with the size of the whole screen and then perform clipping on this form. This approach, however, is slow. I see some other suggestions about doing a Direct3D hook for drawing overlay, but this is probably too complicated and I'm not sure how stable it is with respect to different Direct3D version. Any ideas how OneNote does it?
I think instead of creating the transparent layer on top of the screen, just grab a screenshot of the whole screen and make it full screen. So users are drawing on a static image not a translucent layer. Since it is a screenshot already, it is already frozen. I think this is how they do it anyway, I doubt an application can simply freeze a screen, they take a photo of it and cover your screen with it, so its as if its frozen.
i am facing problem in drawing dynamically in a picture box. i works very good when the drawing objects are few but as the drawing objects increases the response time of my GUI is getting worse and worse, my GUI works very well up to 90 drawing objects but i have to support more than 1000 so this technique didn't work for me.
know i have decided to adopt layers mechanism, i mean i will draw different layers of drawing in memory and then XOR them to load the final image to my display.
the question is "i Can play directly with memory do draw layers using C# (Examples needed?)"
other ideas are also appreciated,
(Drawing objects means the shapes line,circles etc. that i have to draw on my GUI)
thanx in advance
Whenever I do any custom drawing, I make sure that I draw onto a bitmap and then just display the bitmap. You don't want to have to constantly redraw an image if it hasn't changed.