Android - 如何使用 DragShadowBuilder?

标签 android

我正在使用 DragShadowBuilder.java,这个类是一个示例。

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

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

非常感谢。

最佳答案

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

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 - 如何使用 DragShadowBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32198394/

相关文章:

java - 将图像按钮与 TextView XML 对齐

java - 将时间戳转换为时间会给出错误的时间

Android 应用再次调用 MAIN/LAUNCHER,而不是 onResume()

android - 在没有 ActionBarSherlock 的情况下在 Actionbar ShareActionProvider 上设置自定义共享图标

android - 即使在使用 CordovaInterface.getThreadPool() 之后,Cordova Exec 也会阻塞主线程

android - Galaxy S3 Android WebView 中的 Signal 11 SIGSEGV 崩溃

android - Activity 已泄漏窗口

javascript - 如何设置 ListView 的每个单元格具有单独的白色背景?

android - onClickListener 和 onItemClickListener 的区别

Android Image Picasso Square缓存大小