Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onDraw() on View draws behind the layout

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?

like image 867
Nick Avatar asked Jan 28 '11 11:01

Nick


People also ask

What is onDraw method?

Override onDraw() The parameter to onDraw() is a Canvas object that the view can use to draw itself. The Canvas class defines methods for drawing text, lines, bitmaps, and many other graphics primitives. You can use these methods in onDraw() to create your custom user interface (UI).

Which methods will always cause the view draw () method to be called?

The onDraw method is called whenever android thinks that your view should be redrawn. This can be tha case when your view is animated, in which case onDraw is called for every frame in the animation. It is also called when the layout changes and your view is re-positioned on the screen.

How Android draws views?

Drawing is handled by walking the tree and rendering each View that intersects the invalid region. In turn, each ViewGroup is responsible for requesting each of its children to be drawn (with the draw() method) and each View is responsible for drawing itself.

How to draw shapes in Android studio?

In order to draw your shape, you must compile the shader code, add them to a OpenGL ES program object and then link the program. Do this in your drawn object's constructor, so it is only done once.


1 Answers

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);
}
like image 143
Nick Avatar answered Sep 21 '22 07:09

Nick