我试图将一张图片拖放到另一张图片上。到目前为止,我得到阴影工作,当用户拖动图像时,它做了一些事情。我想要做的是在拖动图像时使图像可见,因为只显示阴影。
尝试使用此how to drag and drop a image onto a image但无法理解。我想我的剪辑数据有误。
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.imagedrop.MainActivity" >
<ImageView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/v"/>
<ImageView
android:id="@+id/target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/view"
android:layout_centerHorizontal="true"
android:layout_marginTop="118dp"
android:src="@drawable/ic_launcher"
android:contentDescription="@string/v" />
</RelativeLayout>
Java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get image
findViewById(R.id.view).setOnLongClickListener(longListen);
findViewById(R.id.target).setOnDragListener(dropListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
OnLongClickListener longListen = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//dragshadow created
DragShadow dragShadow = new DragShadow(v);
// clip data
ClipData data = ClipData.newPlainText("", "");
v.startDrag(data, dragShadow, v, 0);
return false;
}
};
OnDragListener dropListener = new OnDragListener() {
public boolean onDrag(View v, DragEvent event)
{
int dragEvent = event.getAction();
switch (dragEvent) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i("Drag Event", "Started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Drag Event", "Entered");
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("Drag Event", "Exited");
break;
case DragEvent.ACTION_DROP:
//sends to new activity
break;
}
return true;
}
};
private class DragShadow extends View.DragShadowBuilder {
//greybox drawable made
ColorDrawable greyBox;
public DragShadow(View view) {
super(view);
greyBox = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onDrawShadow(Canvas canvas) {
greyBox.draw(canvas);
}
@Override
public void onProvideShadowMetrics(Point shadowSize,
Point shadowTouchPoint) {
View v = getView();
int height = (int) v.getHeight() / 2;
int width = (int) v.getWidth() / 2;
greyBox.setBounds(0, 0, width, height);
shadowSize.set(width, height);
shadowTouchPoint.set((int) width / 2, (int) height / 2);
}
}
}