【问题标题】:Android - How to use DragShadowBuilder?Android - 如何使用 DragShadowBuilder?
【发布时间】:2015-08-25 18:14:18
【问题描述】:

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

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

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 并传递要在其构造函数中拖动的视图实例。如果您愿意,我可以举一个使用它作为答案的示例吗?
  • 是的,是的,我需要。我不知道应该向 DrawableDragShadowBuilder 的构造函数发送什么参数。
  • 不要使用上面的例子...而是使用一个非常简单的例子,我会为你发布。

标签: android


【解决方案1】:

你必须在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;
  }
 }
} 

【讨论】:

  • 如果您有我的代码示例,请获取我的链接。非常感谢。
【解决方案2】:

我为我的问题找到了一个示例:

我的StartActivity.java

    public class StartActivity extends Activity implements View.OnLongClickListener{

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_start);
            findViewById(R.id.imgv).setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            View.DragShadowBuilder shadowBuilder = new DrawableDragShadowBuilder(v, 
            getResources().getDrawable(R.drawable.ic_launcher));
            v.startDrag(null, shadowBuilder, ((ImageView) v).getDrawable(), 0);
            return true;
        }
    }

还有activity_start.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StartActivity">

    <ImageView
        android:id="@+id/imgv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2015-03-28
    相关资源
    最近更新 更多