15

Ok, here's the deal. I want to move items in my extended gallery class to change the order of images. The way i'm doing it now:

  • on long press remove the current selected item,
  • use onDraw() to draw the same image so i can move it around using onTouchEvent()
  • on release add the item again

This works fine but the problem is that when using the onDraw() method it will draw the image behind the gallery items. Is there a way to change the priority of what is drawn?

Nick
  • 1,733
  • 1
  • 14
  • 24
  • Are you calling super.onDraw() after actually drawing the image ? – pankajagarwal Jan 28 '11 at 13:32
  • Well the image in the gallery are drawn by the gallery, so i don't really have control over that. i just want to draw over those images from within the extended gallery class – Nick Jan 28 '11 at 13:59

1 Answers1

41

Well i found this out after going into a totally different direction =/

Here's the solution for people that have the same problem:

In constructor (or anywhere else you initialize the component) set setWillNotDraw(false) and override dispatchDraw(). dispatchDraw() draws the ViewGroup children so you can decide yourself if you want to draw behind or a top of the other views.

Example taken from Custom drawing on top of Gallery view (and it's child views)

@Override
protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);

    // do your drawing stuff here
    canvas.drawPath(mPath,mPaint);
}
Community
  • 1
  • 1
Nick
  • 1,733
  • 1
  • 14
  • 24
  • 3
    +1 as this information is hard to find by searching. Used this approach successfully to draw onScreen help on top of a GridView. Minor typo: setWillNotDraw(false) should be called; willNotDraw() is the getter for that same parameter. N.B.: setWillNotDraw(false) disables optimizations that assume that a ViewGroup relies entirely upon its child views to draw itself. – Carl May 31 '12 at 16:16
  • Please share more code sample. Its really easy to understand by looking at code. +1 both – AZ_ Jul 02 '12 at 08:38
  • Nick do you still remember this? do you override dispatchDraw() on the images of the gallery or in the custom view? Or you are doing all in one view? Any help appreciated – quinestor Jun 19 '13 at 23:23
  • Ha this was a long time ago. I think i just get the child in the custom gallery, grab the bitmap of that, hide the child and draw the bitmap in dispatchDraw() – Nick Jun 20 '13 at 07:58
  • This is an especially useful technique for drawing an optional selection indicator (perhaps a rounded stroke rectangle) on top of a custom view. – Groovee60 Aug 15 '15 at 19:45
  • You don't need to do `setWillNotDraw(false)`, worked for me without that. – Peter Mitrano Jul 10 '17 at 03:10