A have a custom view. This view works on principle ListView, but all items draws on Canvas, so they are not are children of ViewGroup. Say more: my View is not a ViewGroup, it extends View. Now I want to use Adapter interface to provide data to my view via getView(...) method. But. I have been stucked with drawing issue. I trying to change my extending from View into ViewGroup and here is my confusing. onDraw() method is not called! I really want to avoid posting my code, that works under NDA, so I want to know - is the ViewGroup drawing logic not the same if we extends from View, not from ViewGroup?
that's the expected behavior, unless the flag setWillNotDraw is forced to false. ViewGroup are not expected to do any drawing on their own, but to delegate the drawing to their children. If you want to override this behavior, you can either call setWillNotDraw(false) or override dispatchDraw(Canvas canvas) instead of onDraw
Related
How I can determine if a view was invalidated? I see that the View class has isDirty() method but it doesn't seem to work in my case.
I'm making a custom layout that can animate blurring of it's content. It extends FrameLayout. So at first the layout draws it's first child to a bitmap, and than displays the bitmap instead of actual view. Also it starts AsyncTask that creates blur animation steps.
So I need to know when the child view was changed so I can redraw the bitmap and restart AsyncTask.
I've found that this can be implemented using ViewTreeObserver.OnPreDrawListener
I was just reading few stackoverflow threads about scrolling views larger than parents. Everywhere is used ScrollView or HorizontalScrollView for this, but what is puzzling me is why does every single view have methods and xml attributes for setting scrolling? For example, Im currently overriding method onScrollChanged() of View class. I mean can every view have a scrollbar?
Because View is a class/Object.
To give you a more to the point example here you go:
public class Test extends View {
}
Therefor, it's pretty much obvious for View to have methods for each instance of the object.Right?
We're trying to write our own MapView, and I'm trying to figure out how adding overlays to the mapview causes them to get drawn in other mapping APIs
I've got a MapView that extends ViewGroup. I think I've figured out that calling MapView.invalidate() causes the mapview's dispatchDraw method to be called. Does that sound correct?
If that's true, when is the onDraw and draw method of the mapview called?
More importantly is all this view and which methods called when stuff documented well anywhere?
Thanks!
EDIT
This SO post explained that for classes that extend ViewGroup, the onDraw method is not called automatically. You have to force it if you need it. But as ebarrenchea pointed out, the order is draw, onDraw, dispatchDraw IF all the methods are called
Calling invalidate on your viewgroup will force draw to run which will in turn call onDraw and then dispatchDraw. You should have a look at the view documentation here and the View source code here for more information.
invalidate() has to be called from UI thread to cause onDraw(). Try to use postInvalidate() which should have the same effect as invalidate(), but works from non UI threads.
I've a custom ViewGroup, to which a custom ImageView is added as a child. Now, my problem is that whenever I change the image in the child, by overriding the onDraw(), the onLayout method in the parent is called. Is it not possible to just change the image in the child without calling the onLayout method in the Custom ViewGroup? Thanks.
Interestingly, no one answers this simple question.
The onLayout() method is called by default when the viewgroup needs to be redrawn.
see https://developer.android.com/guide/topics/ui/how-android-draws.html
you can actually extend the viewgroup to implement your custom onLayout() and onMeasure() method to control the behavior it is called.
I want add customized View to ScrollView. In customized View, Only two methods are described, constructor and onDraw(). But the image that drew in onDraw(), isn't show on ScrollView. What's wrong in my program?
You might need to override onMeasure() so the view can be sized appropriately. Otherwise it might help if you post some code with your question.