0

I would like to create a custom drag shadow that is different from the view being dragged (actually it is a modified version of the view being dragged).

I can pass a view to the View.DragShadowBuilder constructor like so:

View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(myView);

Problem is any changes I make to myView also affects the View being dragged (and this is because Views cannot be cloned). How do I clone a View?

Alternatively I can extend the View.DragShadowBuilder and draw my custom view with a Bitmap in the provided onDraw(Canvas canvas) method. But I am having trouble generating a shadow at all in the first place (I am assuming this is a valid approach).

Conversely is it possible to make a custom drag shadow at all from a View attached to the screen ? Any pointers or a solution would be most helpful.

My searches so far haven't yielded any useful results: How to create a custom drag shadow? How to show custom DragShadow instead of ListViewFragment row?

user1841702
  • 2,683
  • 6
  • 35
  • 53

2 Answers2

3

Answering own question. The argument passed to View.DragShadowBuilder(View view) needs to be visible (and attached to a suitable layout container). An inflated view from xml or a dynamically created view that is not attached to any ViewGroup / View does not generate any drag shadow.

The key here is to extend the View.DragShadowBuilder class and override the onDrawShadow(Canvas canvas) method

@Override
onDrawShadow(Canvas canvas) {
    final View view = mView.get();

    /* give drag shadow a blue background, this also changes the drag-start view
    attached to the screen */
    view.setBackgroundColor(Color.BLUE);

    view.draw(canvas);

    /* bring the drag-start view back to its original state (does not affect drag-shadow) */
    view.setBackgroundColor(Color.TRANSPARENT);
} 

If your requirement is to change the drag shadow so that it appears different from the drag-start view attached to the screen, modify the view before you call the view.draw(canvas) and bring it back to its original state after the view.draw(canvas) method.

This gets the job done, but comes across as a hack. To summarize the key here is to manipulate the view before the draw(canvas) method and reset it to its original state after the same method. Hope this was useful.

user1841702
  • 2,683
  • 6
  • 35
  • 53
-1

View.updateDragShadow is exposed for this, SDK version >= 24. I guess your solution is better because it's much more flexible.

ShaMan123
  • 99
  • 4