我目前正在尝试使用拖放事件来确保可以在屏幕上的任何位置拖放特定视图。所以我在http://www.tutorialspoint.com/android/android_drag_and_drop.htm
咨询了代码但是正在发生的事情是拖动工作完美,但是当触发放置事件时,视图不会放置在布局上,也不会放在窗口的任何位置。 虽然我特别想让它在屏幕上的任何地方都可见,但我们的想法是将imageview拖动到布局顶部,id =“@ + id / Layout” 我的代码是
activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.example.user.gesturedemo.Activity">
<RelativeLayout
android:id="@+id/Layout"
android:layout_height="300dp"
android:layout_width="match_parent">
<ImageView
android:id="@+id/anImageView"
android:layout_height="300dp"
android:layout_width="match_parent"
android:background="@drawable/signboard"
android:contentDescription="Image" />
<ImageView
android:id="@+id/aImageView"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="@drawable/letter"
android:layout_centerVertical="true"
android:layout_marginLeft="40dp"
android:contentDescription="Image"/>
</RelativeLayout>
<com.example.user.gesturedemo.ObjectImageView
android:id="@+id/objectImageView"
android:layout_height="50dp"
android:layout_width="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:background="@drawable/a"
android:layout_marginLeft="40dp"
android:contentDescription="Image"/>
活动类有
public class AnActivity extends AppCompatActivity {
private static final String msg = AnActivity.class.getSimpleName();
private com.example.user.gesturedemo.ObjectImageView alObject;
private RelativeLayout canvas;
private android.widget.RelativeLayout.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blackboard);
canvas = (RelativeLayout) findViewById(R.id.canvas);
alObject = (com.example.user.gesturedemo.ObjectImageView) findViewById(R.id.alphabetObjectImageView);
alObject.setOnLongClickListener(mObjectLongClickListener);
alObject.setOnDragListener(mObjectOnDragListener);
alObject.setOnTouchListener(mObjectObjectTouchListener);
// canvas.setOnDragListener(mObjectOnDragListener);
}
private View.OnLongClickListener mObjectLongClickListener = new View.OnLongClickListener(){
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(),mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(alObject);
v.startDrag(dragData,myShadow,null,0);
return true;
}
};
private View.OnDragListener mObjectOnDragListener = new View.OnDragListener(){
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
layoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED");
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED");
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
v.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_LOCATION :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED");
// Do nothing
break;
case DragEvent.ACTION_DROP:
Log.d(msg, "ACTION_DROP event");
Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED");
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
v.setVisibility(View.VISIBLE);
break;
default: break;
}
return true;
}
};
private View.OnTouchListener mObjectObjectTouchListener = new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(alphabetObject);
alObject.startDrag(data, shadowBuilder, alObject, 0);
alObject.setVisibility(View.INVISIBLE);
return true;
}
else
{
return false;
}
}
};
}