I have a custom view (extends View) that is going to be a game board. What I'd like to do now is create an object (well, a bunch of them, but I can start with one) at a specific location on the board and then have the user be able to move it around by dragging it. I have the board view down, but am at a loss on how to code up the "pieces." Thanks for any help and/or pointing to the right place here.
Some pointers:
You will need to capture the user's touch 'n drag. Unfortunately you can't use a GestureDetector.SimpleOnGestureListener for this (it would see it as a scroll), so you'll have to do this in your view's onTouchEvent. This is a little tricky - refer to the SimpleOnGestureListener source code for ideas on how best to do this.
As you update the location of the object call invalidate() on the view to trigger the re-draw
In onDraw() do the background and then draw your object on top
If you are just doing the one object at once this should be sufficient. If there's more to it that this you may have to graduate to a SurfaceView and a separate thread - but don't go there until you need to.
Related
I´m new in android development. I want to create a game where the user needs to quickly drag and drop balls with the finger in the screen to specific locations in order to win.
I don´t know if i can do this with simple ImageViews or i need to use canvas or other thing to draw in the screen.
I have tried to drag and drop an ImageView on top of another ImageView but it does not seem to work very well and i'm hesitating if i need to do it drawing figures instead of using the ImageViews or to do something else.
Please advice. Any help is apreciated. Thank you.
ImageViews are Views designed to hold image drawables, which isn't really needed in your case - you can use a simple View with a color background or implemended onDraw method, you can use an ImageView just as well.
Asfor the drag and drop, what you need is any View with implemented onTouch method so that the view changes its position on screen with ACTION_MOVE touch events.
If you're interested, i have implemented a simple frame layout with drag and drop functionality. My layout requires long-click to start drag, but it can be easily modified so it won't. Check it out at my github page. You can clone the project and run demo activity to see how it works, and start from there.
I've recently started building my second android app and I've hit a wall. What I want to know in general is this. In almost all other programming languages that I've used there is a simple way of drawing arbitrarily on screen. Usually there is some sort of Scene or Canvas type class and another GraphicObject type class. So If, say, I want to draw a car, I create a class that descends from GraphicObject and define they it draws itself and then add an instantiation of the Car class (the object) to the Scene. If I want a second car, you just instantiate another and put it wherever you like, by setting some kind of coordinate function. If this is a program where what is drawn depends on user input, everything must be done programatically and hence it does not depend on anything other than the code written.
How do you achieve this in Android? As far as I can muster the only object which you can define the way it is drawn is a View. However I have not been able to simply take my custom view (that draws a rectangle in my case) and instantiate two of them a put them in the screen where I want them, and do this in a programtic simple way....
Thank you for any advice.
Well if I want to draw lets say 10 rectangles then I wont instantiate my custom View 10 times. Instead I will add logic in my custom View to be able to draw 10 or more rectangles. Anyway if you want to draw again what was drawn in a View then you can call setDrawingCacheEnabled(true); on your View and then call getDrawingCache() to get Bitmap to draw it somewhere else.
Its almost same in android also,
All the widgets are inherited from class View, by extending this class you can actually create whatever you want limiting by only yours imagination, you have access to canvas here, use it along with touch controls exposed from class itself to override all the behaviours you want as per your need,.
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/graphics/Canvas.html
I have a very specific component to realize. I don't really know how to do it.
The component is a segmented bar with a cursor that can select any segment. A segment selection should update the number of segment in the bar. A segment could have two "state" which is represented with 2 differents graphics.
IE :
This may sound like a rating Bar but i really don't know how to proceed (new with java and android).
Should i use Rating Bar and just change the stars by segments ?
Should i extend RatingBar (how ?) ?
Any help will be appreciated
Looks like you're going to have to write your own view. Don't worry - it's not so difficult (I did my first one without any sweat after 2 days of doing Android). Basically you have a class which extends View and you override the onDraw function which draws directly to the supplied Canvas. Make sure you override the onMeasure function too or your view will have no height!
Have a look at the android page on Building Custom Components - a quick google or stack overflow search will give you many hints as to how to proceed too.
Once you work out how to write a custom view (start with a small prototype) then you just need the logic of how this specific controller works. I'd probably have a collection to hold each of your segments, with each item being a class representing your data. In your onDraw then you just have to process the collection and draw to the screen.
To get your touch to zoom, override the onTouchEventmethod, which will give you a MotionEvent object which you can call getX() and getY() on to return the coordinates of where the user touched your view - from there you can work out what segment the user touched and process the zoom.
Let me know if you need any more help.
This doesnt look to hard to achieve.
Create a custom view, override onDraw() and go from there; each section probably would be a rectangle, and since you know how many sections there are you can divide the with to the amount of sections, and draw them on the correct positions.
Override ontouch and check the motionevent for click locations, do a hit test to figure out which segment is clicked on, then set a boolean zoom to true, invalidate the view such that it gets redrawn and draw just the zoomed segments.
You would probably want to back this up by a simple array containing the state for each of the sections.
I want to put some graphical shapes (e.g., a character, some moving objects, etc.) in a big view, and being able to touch them.
What should I use ?
- A drawable object is simple to draw but it can't be attached to a touch listener. How can I detect a click in the bounds of my shape ?
- With views, I can use a touch listener, but is it a good idea to use views to draw graphics in a game ? Aren't they better for forms ? Also, I want to put those shapes anywhere on the screen (absolute position). I cannot find a way to do this with views.
Thank a lot.
It sounds like you would want to draw the objects within a single view (your drawable object method) and then use a touch listener on that encompassing view. In that listener, you would compare the touch position to the positions/bounds of the objects you are drawing - and if the touch would qualify as being 'on' an interactive object, you would then handle it accordingly.
I am recently getting into Android programming and want to make a simple game using 2D canvas drawing. I have checked out the Lunar Lander example and read up on some gestures, but it looks like I can only detect if a gesture occurred. I am looking to do a little more complicated detection on a swipe:
I want to make a simple game where a user can drag their finger through one or more objects on the screen and I want to be able to detect the objects that they went over in their path. They may start going vertically, then horizontally, then vertically again, such that at the end of a contiguous swipe they have selected 4 elements.
1) Are there APIs that expose the functionality of getting the full path of a swipe like this?
2) Since I am drawing on a Canvas, I don't think I will be able to access things like "onMouseOver" for the items in my game. I will have to instead detect if the swipe was within the bounding box of my sprites. Am I thinking about this correctly?
If I have overlooked an obvious post, I apologize. Thank you in advance!
I decided to implement the
public boolean onTouchEvent(MotionEvent event)
handler in my code for my game. Instead of getting the full path, I do a check to see which tile the user is over each time the onTouchEvent fires. I previously thought this event fired only once on the first touch, but it fires as long as you are moving along the surface of the screen, even if you haven't retouched.