Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to use DragShadowBuilder?

Tags:

android

I am using from DragShadowBuilder.java, this class is for a sample.

I don't know how I can use in my Activity, What parameters I should send to constructor of DrawableDragShadowBuilder:

public class DrawableDragShadowBuilder extends DragShadowBuilder {
    private Drawable mDrawable;

    public DrawableDragShadowBuilder(View view, Drawable drawable) {
        super(view);
        // Set the drawable and apply a green filter to it
        mDrawable = drawable;
        mDrawable.setColorFilter(new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY));
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point touchPoint) {
        // Fill in the size
        shadowSize.x = mDrawable.getIntrinsicWidth();
        shadowSize.y = mDrawable.getIntrinsicHeight();
        // Fill in the location of the shadow relative to the touch.
        // Here we center the shadow under the finger.
        touchPoint.x = mDrawable.getIntrinsicWidth() / 2;
        touchPoint.y = mDrawable.getIntrinsicHeight() / 2;

        mDrawable.setBounds(new Rect(0, 0, shadowSize.x, shadowSize.y));
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        //Draw the shadow view onto the provided canvas
        mDrawable.draw(canvas);
    }
}

Thank's a lot.


1 Answers

You have to pass the View which you want to drag, in the constructor of DragShadowBuilder

DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);

And then start the drag using:

view.startDrag(data, shadowBuilder, view, 0);

Here is the complete example following this excellent tutorial:

import android.app.Activity;
import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MyActivity extends Activity {



@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
  findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
 findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
 findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
 findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
 findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
 findViewById(R.id.topright).setOnDragListener(new MyDragListener());
 findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
 findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());

 }

private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
  if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
    ClipData data = ClipData.newPlainText("", "");
    DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
    view.startDrag(data, shadowBuilder, view, 0);
    view.setVisibility(View.INVISIBLE);
    return true;
   } else {
    return false;
   }
 }
} 

class MyDragListener implements OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);

@Override
public boolean onDrag(View v, DragEvent event) {
  int action = event.getAction();
  switch (event.getAction()) {
  case DragEvent.ACTION_DRAG_STARTED:
    // do nothing
    break;
  case DragEvent.ACTION_DRAG_ENTERED:
    v.setBackgroundDrawable(enterShape);
    break;
  case DragEvent.ACTION_DRAG_EXITED:
    v.setBackgroundDrawable(normalShape);
    break;
  case DragEvent.ACTION_DROP:
    // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    ViewGroup owner = (ViewGroup) view.getParent();
    owner.removeView(view);
    LinearLayout container = (LinearLayout) v;
    container.addView(view);
    view.setVisibility(View.VISIBLE);
    break;
  case DragEvent.ACTION_DRAG_ENDED:
    v.setBackgroundDrawable(normalShape);
  default:
    break;
    }
  return true;
  }
 }
} 
like image 189
Sharp Edge Avatar answered Jan 23 '26 22:01

Sharp Edge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!