There seems to be a lot of classes, which provide a surface to draw on:
View
Bitmap
Drawable
SurfaceView
My application generates many pictures dynamically, by using vector graphics.
What is the right class to use, if my aim is to save the pictures, so that they are ready to be drawn into an ImageView?
The answer is described here, in the first paragraph:
- use Views for drawing static items
- use SurfaceView for dynamic and performance sensitive apps, like games
Related
I am drawing some dots to represent players of two teams on a map.
Each team has its own colour.
Important to note that the dot contains two circles, outer border and a inner fill so there will be two colours, with the border always being the same.
It makes sense for me to generate this at runtime rather than packing a texture for each combination.
Upon research, there seems to many ways to achieve this but each has a associated problem
ShapeRenderer
ShapeRenderer is for debugging purposes and should not be used for usual drawing as stated by a LibGdx developer here
http://badlogicgames.com/forum/viewtopic.php?t=8573&p=38930
For this reason I avoided using this
Pixmap
This was very promising, I liked the idea that I could just generate two textures and re-use them for each sprite. The biggest problem with this is that Textures made via Pixmap are un-managed so if the OpenGL context is lost and regained (This can be easily reproduced in an Android application, if the user backgrounds the app and restores it from foreground). I am primarily targetting Android so this an issue for me
Texture Re-Colour
Was thinking I could create a grey scale dot and re-colour it but since my asset has two parts to it, I am not sure how I could selectively choose the inner circle and fill it.
Question 1 How Do I Restore Pixmap Texture On Context Loss?
I have not found an example which details how to do this? I assume it is going to be done in the resume lifecycle callback but what do I need to do?
Question 2 Alternative Way?
Is there an alternative way for my issue perhaps?
Thanks for reading!
Load just one texture with white circle. Use SpriteBatch to draw players: first call batch.setColor(borderColor) and draw the circle Texture with outer radius, then call batch.setColor(fillColor) and draw it with inner radius. Sure there is a some performance impact because of drawing fill part twice, but if circles are small enough the impact is going to be negligible.
I'm building an android game and this is more of just best practices and performance question.
For the bitmaps that I have that the user interacts with, should I place them in an imageView and set an onTouchListener for each of them individually or should I just draw them onto the canvas and use the custom view's onTouch method to obtain the x and y of the touch and see if it falls in the range of any of the bitmaps to detect a touch.
My custom view takes up the entire screen, and I don't know how if it is even possible to draw an imageview onto the screen using a canvas which is why as of now I just use the onTouch method.
Thanks for any insight.
Depending on how dynamic your bitmaps render will be, you should go for either GLSurfaceView or SurfaceView, for something simple as just bitmaps i would recommend you to use SurfaceView as "renderer" where you can get the canvas from and of course you can draw on the whole screen if your surfaceview match the screen size.
TouchListener should be completely separated handled on its own listener promoting encapsulation and reuse of code for future apps that you want to do. I've found this quiet helpful for the last games i've developed asingning a surface view as renderer and just creating Objects which takes the canvas as parameter and drawing them self into it, the only thing you have to take on count is the bitmap resource management, but if you are careful of releasing and creating them when is proper, you should have no problems...
Regards!
I'm converting a project that I wrote in AIR a long time ago to native Android. In AIR, positioning views was fairly easy in x,y coordinate systems. With native Android though, I am trying to approach this in a correct way, but I'm unsure how to approach.
My view will consist of two circles in the background, with small objects within those circles that can roll around. There will be another view drawn on top of the circles to make it seem like there is glass over the circle, entrapping the small objects. The small objects are bound to the background circles.
I guess what I'm really asking is canvas drawing the best approach here, or is a view-based layout workable as well?
It sounds like your best option would be to use a SurfaceView. This is basically a hardware accelerated canvas. On of the benefits is that you can overlay standard widgets over top of it if you need to so you can mix and match custom and standard components. Here is a link to a website that walks you through getting a SurfaceView up and running
I am currently attempting to animate some images in my SurfaceView. Right now, I am displaying my images as Bitmaps. However, I think it might be better to implement these Bitmaps as ImageViews. I have not been able to create multiple ImageViews on my SurfaceView (I am trying to not do this in XML, as I want to get things working first and then later change things around so it is more portable). Is there a tutorial somewhere on how to do this, or am I like wayyy off and should just stick to Bitmaps??
You generally shouldn't try to mix View objects and SurfaceView. SurfaceView is a very special view that provides direct drawing by essentially punching a hole in the current window. It seems better to stick to bitmaps.
I am trying to make a game for the android. I currently have all of my art assets loaded into the drawables folder, but my question is how do I actually reference a specific one to render it?
I know that the files each have a unique #id, and I probably have to implement in the #override of the onDraw(canvas) method. so the question is how do I actually display the image to the screen?
I have looked through a good half dozen books and all those talk about is getting an image off the web, or manually drawing it with the paint functionality, but I have the assets ready to go in .bmp, and they are complex enough that coding paint to draw them by hand will be a very great migrain.
I would prefer a direction to look in (specific book, tutorial, blog, open source code[assuming quality comments])I am still kinda learning java as my 2nd programming language and so I am not to the point of translating pseudocode directly to java yet.
[added in edit]
does drawing using Paint() every frame have a lower overhead then rendering a bitmap, and only changing the display when it changes?
for 2Dgames I would recommend you to use SurfaceView
load your image as a bitmap and let the canvas draw that bitmap.
to animate the images there are two possible way :
You could inflate the view
Create a looping thread to call the draw function
Here's some good starting tutorial about displaying image in Android, especially for games
You can use one of the drawBitmap variants of the Canvas class. A canvas object is passed in the onDraw method. You can use the BitmapFactory class to load a resourse.
You can read a nice tutorial here.