Android Games specifically bitmaps - android

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!

Related

Best solution SurfaceView vs View drawing?

I would like to display the score when user click on a button (make +1 fly to top of the screen) and I wonder if it is better to use a surfaceview or view?
See here for more information on the differences between SurfaceView and View.
For your case, View would probably be simpler and appropriate.
If your application does not require a significant amount of processing or frame-rate speed (perhaps for a chess game, a snake game, or another slowly-animated application), then you should consider creating a custom View component and drawing with a Canvas in View.onDraw(). The most convenient aspect of doing so is that the Android framework will provide you with a pre-defined Canvas to which you will place your drawing calls.
A SurfaceView, on the other hand, is not drawn on the main thread and instead takes place on a secondary thread:
The SurfaceView is a special subclass of View that offers a dedicated drawing surface within the View hierarchy. The aim is to offer this drawing surface to an application's secondary thread, so that the application isn't required to wait until the system's View hierarchy is ready to draw. Instead, a secondary thread that has reference to a SurfaceView can draw to its own Canvas at its own pace.
I think view fits this scenario better. You could translate a view to the top of the screen.
So far, I've only used SurfaceView for rendering background graphics. Of course you could do it using SurfaceView, but that would bring more complexity in terms of coding. You need to draw this "+1" and calculate its position from frame to frame.
Using a View, you could just apply an animation, set the duration, and that should do the trick.

How to implement item touches on SurfaceView

I'm developing an android game with the standard pattern of SurfaceView, SurfaceHolder, Canvas, etc. In this game I will be drawing multiple bitmaps on the canvas that I would like to know if they've been touched. I am aware that I can override onTouchEvent() for SurfaceView and could linearly walk through all my items to determine if the touch coordinates contains the area of one of my bitmaps, but I was wondering if I could make things easier on myself.
Here's my question:
Can I have classes which handle drawing my bitmaps on the screen descend from android.View, attach them to my SurfaceView and register click listeners on them so I don't have to go through the process of determining clicks myself? Will the click listeners be accurate if I am drawing the views (bitmaps) to the canvas myself?
Furthermore, would I suffer a performance hit from making all of my bitmaps android views by bogging down the android view hierarchy and making android do the work I didn't want to?
Thanks.
If you want to attach normal android views to SurfaceView then what is the point in using SurfaceView! You are given a SurfaceView so that you can draw on the surface yourself rather than having the android view system do it for you. So "you" have to keep track of your touches by yourself.
You can refer to AOSP Gallery2 code which is purely OpenGL based and incorporate its design on handling touches.

How to drag multiple bitmaps on Android Surfaceview

I am using a SurfaceView and have drawn multiple bitmaps to the view. My question is how can I use the Touch Listener to target each bitmaps move action independently?
Basically I want the user to be able to arrange the bitmaps in any order they like. I came up with a method of determining the space utilized on the SurfaceView for each bitmap and then checking the x and y values of the touch listener to determine if the user was selecting a known location of the bitmap. The problem I am having is that if bitmaps end up crossing each other then they stick together as they share the same space.
I am thinking there is probably a simpler way to handle the events of multiple bitmaps, any suggestions?
First you need to select the bit map
follow the link for further detail.
Detect touch on bitmap

Draw an image floating over another in Android

I want to be able to dynamically place image(s) over another image in my app.
Consider the first image as background and the other images to be on top level, I will also need to move those top level images (change their x and y on the screen) by code too.
Imagine, for example, a sea in which the user places fish and sea animals, then those sea animals start to move here and there on the screen: it will be like that.
How can I do this? If you don't know but remember any simple program or demo that does that, it will be also very welcome!
Thank you!
There is, of course, more than one way to do this, but I would say that the best way to do it would be to create a custom View (class that derives from View) and have this handle your bitmap drawing and all of your touch events.
There's a lot of code to write for loading the Bitmaps and keeping track of all of their positions (and then drawing them to the canvas in onDraw), but if you start really small by just allowing one image to be drawn and dragged around the screen, you can build on that and keep your code organized.
You would need to override onDraw(Canvas) and onTouchEvent(MotionEvent) in your custom View. You'll load your bitmaps with BitmapFactory (decodeResource method if you're including your images as resources in your project) and you'll need to remember to call recycle on your bitmaps when you're no longer using them.
In onDraw, you draw your bitmaps to the canvas at a specific location using Canvas.drawBitmap. There are two overloads of this method you can choose from, one that takes the top and left coordinates of the bitmap as floats (and therefore performs no scaling or stretching) and one that takes a destination and source rectangle to perform scaling, stretching and placement.
I always use the latter as it gives me finer tuned control. If you choose this route, you'll want to keep two Rect instances and a Bitmap instance for each image being drawn, update them in the touch events and draw them to the canvas in the draw event.
When something changes inside your view (as in the case of a touch event), call invalidate() method and the framework will know to redraw everything which triggers your onDraw method.

How to play animations in a SurfaceView in Android

I have a SurfaceView which is currently drawing a series of Bitmaps. These bitmaps can easily be manipulated geometrically (scale/rotate/translate/etc). An example would be a ball which is bouncing, which may just use translate/rotate operations. The ball is just a single bitmap.
I wish to replace this 'ball' Bitmap with a sequence of Bitmaps, for example if the ball were to explode in a flurry of fireworks, I have the individual frames and I wish to string them together.
Without having to manually cycle through them on each onDraw() call in my SurfaceView, how can I go about this? I know in normal conditions I can just make an AnimationDrawable using the frames in an XML file, bind it to an ImageView (set as background) then call it. However, I am using a SurfaceView for displaying all my graphics on screen, not ImageViews. I have read you can overlay other views on top of SurfaceViews, but I am unsure that I could get the exact position required. For example, the ball may explode at X=65 Y=21 on my SurfaceView, how could I translate this exact co-ordinate to my ImageView?
Any help or tips on alternative methods would be greatly appreciated.
If you want to keep it more separated, you can create another class that handles this animation and keeps track of which frame it's on and the bitmaps to use then delegate drawing of the sprite to this class in your SurfaceView's onDraw.

Categories

Resources