问题
hello and merry Christmas to everyone for tomorrow(UK), I'm rather new to android but have successfully managed a few bits and pieces what im trying to achieve right now is a simple drag and drop activity that allows the user to drag and drop a shape ImageView onto another ImageView, if the image matches then it should replace the image it overlapped, and if it doesnt it should snap back to where it was, i know this means creating an if/else block in my drop event but after thumbing few many tutorials i couldn't piece together what i wanted and i dont currently have enough java knowledge to pull this off currently i have a layout which holds 6 image views, 3 are static and the other 3 can be moved on the screen and placed on the layout but not on the imageviews, i think this is because ive deisgned the draglistener to my layout and not to my imageviews but im a little lost, can somebody help me? thank you for any and all suggestions. here is my code
DragandDrop.java
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;
import android.app.Activity;
public class DragandDrop extends Activity implements OnTouchListener, OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draganddrop);
findViewById(R.id.squareImage).setOnTouchListener(this);
findViewById(R.id.circleImage).setOnTouchListener(this);
findViewById(R.id.triangleImage).setOnTouchListener(this);
findViewById(R.id.top_container).setOnDragListener(this);
findViewById(R.id.bottom_container).setOnDragListener(this);
findViewById(R.id.squareImage1).setOnDragListener(this);
findViewById(R.id.circleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage1).setOnDragListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
LinearLayout to = (LinearLayout) v;
to.addView(view);
view.setVisibility(View.VISIBLE);
}
return true;
}
}
draganddrop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".draganddrop"
android:background="@drawable/dragshapes"
android:orientation="vertical"
android:id="@+id/dropLayout">
<LinearLayout
android:id="@+id/top_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:contentDescription="@string/square_text_content"
android:id="@+id/squareImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragsquare1" />
<ImageView
android:contentDescription="@string/circle_text_content"
android:id="@+id/circleImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragcircle1" />
<ImageView
android:contentDescription="@string/triangle_text_content"
android:id="@+id/triangleImage1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragtriangle1" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:contentDescription="@string/square_text_content"
android:id="@+id/squareImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragsquare" />
<ImageView
android:contentDescription="@string/circle_text_content"
android:id="@+id/circleImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragcircle" />
<ImageView
android:contentDescription="@string/triangle_text_content"
android:id="@+id/triangleImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
android:src="@drawable/dragtriangle" />
</LinearLayout>
</LinearLayout>
回答1:
Remove setting drag listeners for the containers
findViewById(R.id.top_container).setOnDragListener(this);//remove this
findViewById(R.id.bottom_container).setOnDragListener(this);//remove this
Check whether the dragged image view matches with your dropped view.If they are matching set the dropped image view's background to that of the dragged one.(Other dimensions are same as per your xml file) sample code-
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if(view.getId()==R.id.squareImage1 && v.getId()==R.id.squareImage)
{
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragsquare1);//TODO: change this pseudo code.
return true;
} else if(view.getId()==R.id.circleImage1 && v.getId()==R.id.circleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragcircle1);//TODO: change this pseudo code.
return true;
} else if(view.getId()==R.id.triangleImage1 && v.getId()==R.id.triangleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(@drawable/dragtriangle1);//TODO: change this pseudo code.
return true;
} else {
return false;
}
}
}
Hope this helps you..merry christmas
来源:https://stackoverflow.com/questions/20759600/how-to-drag-and-drop-a-image-onto-a-image