I have a custom view which I override the onDraw method to draw bitmaps using the canvas.
I can't really post any code because its quite long and complicated.
I put a log in the onDraw method, and I see that it never stopped being called.
My question is, what can trigger the onDraw method except calling invalidate on the view or its ancestors?
found the issue. I called getWidth() and getHeight() in the onDraw() method
Related
There are a lot of similar questions and therefore this may seem like a duplicate but please read first.
I have gone through all of the following questions and it didn't work for me
onDraw() is not called in custom View
invalidate not redrawing view on api 25
Android Custom Layout - onDraw() never gets called
view.invalidate() not working to redraw imageview
Why onDraw is not called after invalidate()?
Android View Canvas onDraw not performed
Let me clarify further
I am extending View class not any ViewGroup like LinearLayout etc
when I call invalidate() from onTouchEvent it runs fine
when I call invalidate() from another function of the same class (undo()) invalidate() doesn't work
I also tried calling postInvalidate() and also called invalidate using postDelayed(new Runnable ... invalidate() nothing seems to work.
after touching undo button when I touch the view it draws the expected result immediately.
Any suggestion on what could be the issue?
code looks something like below
public boolean undo() {
if (undoStack.isEmpty()) return false;
redoStack.push(undoStack.pop());
if (redoStack.size() >= MAX_STACK_SIZE) redoStack.removeElementAt(0);
// invalidate();
postInvalidate();
return true;
}
here undoStack holds the previous bitmaps that were drawn
I can say that undo and onDraw are working fine as I get the expected results when I touch the view after clicking on undo button. Also I am calling this function from the main Thread and not background thread or async task.
Any suggestions please. Thanks in advance.
i'm building my interactive map and i find myself really confused:
I have an extend of ItemizedOverlay class which i'm managing the overlays.
when shell i use its populate() function in order to update the overlays in the mapView and when should i use invalidate() or postInvalidate() in order to get the map to update.
i found that sometimes this works, and sometimes the other.
if someone will be kind to explain exactly what each of the function does - i will be thanks full because i couldn't find a decent explanation.
populate() should be used when you want to populate the overlay. If all you want to do is have it redrawn, then you should use invalidate() if you're on the UI thread, and postInvalidate() if you're on another thread. The invalidate methods are applicable to any View, be it a button or a text view or anything. They basically tell Android that something has changed related to the View, like the data being shown or the button's state or colour or whatever. Android will then attempt to redraw the View as soon as possible by calling the View's onDraw() method.
I always use invalidate() and never had a problem with it... (i.e. always refreshes)
I implement a view by myself, the onDraw method of the view is called repeatedly. This will cause a serious performance problem. The code is so complex that I can't paste here. Anyone can tell me some possible reasons? I haven't touch the view by my finger.
One reason would be that your onDraw method is calling some method that (perhaps indirectly) invalidates the view. Another possibility is something completely unrelated in your program is causing the repaints. Try this experiment: comment out your drawing code and substitute something really simple, such as a simple line draw, just to show that it's working. If the repeated drawing goes away, then it's something in your onDraw code. Just start restoring the current code a bit at a time until the problem reappears and then you can track it down.
If the problem doesn't go away when you try the experiment, then look somewhere else in your code for something (perhaps a worker thread?) that is invalidating your view.
I had the same problem today. I solved it by overriding invalidate() in my View subclass:
#Override
void invalidate() {
super.invalidate();
}
Then I set a breakpoint inside invalidate() and waited for it to hit. When it did, I had a stack trace of the call, and the offending caller. Mystery solved in 2 minutes.
If that doesn't work, trying overriding the other invalidate methods in View.
I have a custom view which extends View. I am adding this to an activity as setContent(..) after creating its object, but its onDraw() is not getting called more than once. I know that it should be called infinitely.
Any idea why this is happening?
Android 2.2,2.3 tends to call onDraw a lot more than 3.0 and up.You should call onDraw yourself ,or invalidate() , when you want to create an animation or something and don't call it when you don't.
I wanted to know whether the onDraw method get called without the programmers knowledge. I know that it is called at the first time View is loading and I know it calls when I call invaliade(). But does it calls in any other times?
Yes, whenever a parent view is redrawing itself like if the custom view is within a ScrollView whenever you scroll it...