我正在从DragShadowBuilder.java使用,该课程仅供参考。

我不知道如何在Activity中使用,应该将哪些参数发送到constructorDrawableDragShadowBuilder

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);
    }
}


非常感谢。

最佳答案

您必须在View的构造函数中传递要拖动的DragShadowBuilder

DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);


然后使用以下命令开始拖动:

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


这是遵循出色的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;
  }
 }
}

关于android - Android-如何使用DragShadowBuilder?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32198394/

10-10 04:27