3

Can anybody please help me on the following-
1. It should not stick to touch point it should start dragging from any point.
2. DragShadowBuilder view should animate back while its not dropped on correct target.

Pradip
  • 3,189
  • 3
  • 22
  • 27
maheshsgr
  • 2,422
  • 1
  • 14
  • 11

1 Answers1

3

I achieved this by creating a custom View.DragShadowBuilder class.

Code for the same is:

public class CustomDragShadowBuilder extends View.DragShadowBuilder {
View v;
public CustomDragShadowBuilder(View v) {
    super(v);
    this.v=v;
}
@Override
public void onDrawShadow(Canvas canvas) {
    super.onDrawShadow(canvas);

    /*Modify canvas if you want to show some custom view that you want
      to animate, that you can check by putting a condition passed over
      constructor. Here I'm taking the same view*/ 

    canvas.drawBitmap(getBitmapFromView(v), 0, 0, null);
}
@Override
public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) {
/*Modify x,y of shadowSize to change the shadow view 
   according to your requirements. Here I'm taking the same view width and height*/ 
    shadowSize.set(v.getWidth(),v.getHeight());
/*Modify x,y of touchPoint to change the touch for the view 
 as per your needs. You may pass your x,y position of finger 
 to achieve your results. Here I'm taking the lower end point of view*/
    touchPoint.set(v.getWidth(), v.getHeight());
  }
}

For converting the view to Bitmap taken from here:

private Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else 
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

Though this is old question, may prove to be helpful for others who want to use custom DragShadowBuilder.

Comments in the code are self explanatory, for more info let me know. Hope it helps.

Community
  • 1
  • 1
abhy
  • 933
  • 9
  • 13