How to override the OnDraw Method? - android

I have an OnDraw method which draws a set of rectangles [ like an indoor map].On the touch event, a new Activity Intent is called.After the new activity is called,I want to know, how to redraw the rectangles on the buttonClick event( showing a new path in the map).
Below i have mentioned what ive done so far.
1.I have drawn a map on a surface view using DrawRect()
2.Then I catch the rectangle coordinates onTouch event
3.Using a webService, i calculate a shortest path to a location
4.After catching the touched rectangle coordinates, i jump in to a new activity
5.I want to re draw my map(including the path) when i click A button on the new activity
Please give me an idea about how to override and redraw the On Draw method???
Thank you!

If I understand correctly, I think you just need to call View.invalidate() on your view to cause it to re-draw. I'm confused as to why you are switching activities, however. It sounds like you should just do your webservice call in an AsyncTask and then in it's onPostExecute(), call invalidate() on your view to cause it to re-draw with whatever data you have now given it.
However, it sounds like you may not have actually implemented your own View class but instead are drawing directly to the surfaceView? I would read up on how to make your own subclass of View. It's really not that hard.

Related

What is the best way to draw arbitrary objects in Android?

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

Custom Overlay MKMapOverlay iOS port of Android method

I have an android app I have written, that basically has a custom overlay, in this overlay I have overwritten the DRAW method to draw something on a canvas and at the end of my DRAW override, I call the super DRAW passing in the canvas I have painted.. the result is, every time the map is modified in anyway my custom overlay is redrawn as expected.
Now, I am trying to accomplish the same thing on iPhone and am getting a little confused.
Basically I need something drawn in the top right corner of the map every time a redraw occurs of the map. The drawin will change change with every update as well so can't simply be put a view over the map and pass touches through etc.
So, I guess the question is, what is the euivalent of the DRAW method in iOS.. how would I roughly accomplish this same thing? There are MKOverlayView in the API, but there seem to be some significant differences.. So, how do I put something say at 10x10 over the map, whos size is variable and make sure every time the map is moved, scaled or otherwised interacted with this object is redrawn at location 10x10 on the screen.
The docs for MKMapView state that you should not subclass MKMapView, so you can't / shouldn't override the drawRect method. You could add a UIView, though, and override drawRect to do your custom drawing.

Draw a shape that tracks my finger

I know how to draw paths on a canvas and understand how to undo/redo. But now I want to draw shapes (lines, circles, rectangles) that dynamically resize depending on how I drag them - just like we have in MS Paint.
If I use "drawLine", the line is there permanently with no way of erasing it and redrawing it to my new finger location. Same with circle as I want the circle to constantly change width as I drag my finger. So the old one has to erased (keeping all the other paths on the bitmap intact) and the new one drawn in its place.
Been searching a lot for this, but haven't come across how t do it. Maybe I'm using the wrong keywords, but I don't know. Any pointers?
Each time you move the finger, call the underlying view's invalidate() function, it will trigger erasing the entire background
public void invalidate ()
Since: API Level 1
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().
Then redraw your shape based on your finger's new position.
Managed to do it. I misunderstood the way the offscreen drawing thing worked. The idea is to write to the bitmap only after you have the shape you want - ie on "touch up". Till then, draw the shape using the "canvas" object in on Draw...

Android ItemizedOverlay draw method question

Does anybody know the relationship between the members of an Android Itemized Overlay list and when draw() is called. Specifically, I'm trying to find out if draw is called once for each Overlay or once only for the whole set.
Thanks,
R.
I overrode draw() in my ProjectOverlay extends ItemizedOverlay class and in it I loop though the population of the overlay list like so:
for (int ctr = 0; ctr < overlayList.size(); ctr++)
I haven't tested other ways of drawing the screen, because this seems to be working fine.
As far as I understand it, draw() is called once each time the map moves (when you touch the screen and move your finger). By once, I mean that if you want to draw text on each overlay (a name or something) you must loop through your ArrayList in your overriden draw().
As far as I can tell, it's called twice for each visible item. However, I haven't been able to find out which item it's being called for, thus making any recalculation of drawable items difficult (to say the least)

Animation/dragging of an object in Android

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.

Categories

Resources