我想知道是否有任何教程在不同的图像上具有拖放功能,使它们具有特定的目标?
我的意思是,如果你说,4 个可拖动的图像;image_drag_1
, image_drag_2
, image_drag_3
,image_drag_4
和 4 个 drop_target 图像;image_drop_1
, image_drop_2
,image_drop_3
和image_drop_4
.
image_drag_1
应该与 image_drop_1 匹配,并且任何尝试放置image_drag_1
在屏幕布局中的任何其他放置图像或位置上,都会使image_drag_1
快照回到其原始位置。
本教程 实现了 textview 的拖放功能,但没有指定目标。
我已经尝试按照上述教程自行实现我自己的拖放操作,让 textview 可以拖动,而 imageviews 是放置目标。它只是让我的模拟器崩溃。
下面可以找到我的 .java 文件:
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.content.ClipData;
import android.graphics.Typeface;
public class MainActivity extends Activity {
//Imageview to drag the image is dragged into
private ImageView image1, image2;
//textview the image is dragged into
private TextView choice1, choice2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//views to drag
image1 = (ImageView)findViewById(R.id.imageView1);
image2 = (ImageView)findViewById(R.id.imageButton1);
//views to drop onto
choice1 = (TextView)findViewById(R.id.choice_1);
choice2 = (TextView)findViewById(R.id.choice_2);
//set touch listeners
image1.setOnTouchListener(new ChoiceTouchListener());
image2.setOnTouchListener(new ChoiceTouchListener());
//set drag listeners
choice1.setOnDragListener(new ChoiceDragListener());
choice2.setOnDragListener(new ChoiceDragListener());
}
private final class ChoiceTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior
* - clip data could be set to pass data as part of drag
* - shadow can be tailored
*/
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
/**
* DragListener will handle dragged views being dropped on the drop area
* - only the drop action will have processing added to it as we are not
* - amending the default behavior for other parts of the drag process
*
*/
private class ChoiceDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
break;
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
TextView dropTarget = (TextView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//update the text in the target view to reflect the data being dropped
dropTarget.setText(dropped.getImageAlpha());
//make it bold to highlight the fact that an item has been dropped
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view being dropped on - to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
break;
case DragEvent.ACTION_DRAG_ENDED:
//no action necessary
break;
default:
break;
}
return true;
}
}
}
我的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=".MainActivity" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="66dp"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="50dp"
android:paddingRight="50dp" >
<TextView
android:id="@+id/choice_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="46dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="@string/choice_1" />
<TextView
android:id="@+id/choice_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="57dp"
android:background="@drawable/choice"
android:gravity="center"
android:text="@string/choice_2" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_marginBottom="16dp"
android:layout_toRightOf="@+id/imageButton1"
android:contentDescription="@string/image_desc"
android:src="@drawable/firefighter" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/image_desc"
android:src="@drawable/clown" />
关于我做错了什么有什么建议吗?提前致谢。