How To Drag Images From One Imageview And Drop To Another Image View.
public class Quiz extends Activity { int windowwidth; int windowheight; ImageView ima ima2; private View selected_item = null; private int
Solution 1:
For drag and drop please go through the following links .
http://blahti.wordpress.com/2011/10/03/drag-drop-for-android-gridview/
http://www.twylah.com/dodulz/tweets/169426982533206016
Solution 2:
You can achieve it in followin way
publicclassDragAndDropBasicActivityextendsActivityimplementsOnTouchListener {
private ImageView letterView; // The letter that the user drags.private ImageView emptyLetterView; // The letter outline that the user is supposed to drag letterView to.private AbsoluteLayout mainLayout;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (AbsoluteLayout) findViewById(R.id.mainLayout);
mainLayout.setOnTouchListener(this);
letterView = (ImageView) findViewById(R.id.letterView);
letterView.setOnTouchListener(this);
emptyLetterView = (ImageView) findViewById(R.id.emptyLetterView);
}
privatebooleandragging=false;
privateRecthitRect=newRect();
@Override/** NOTE: Had significant problems when I tried to react to ACTION_MOVE on letterView. Kept getting alternating (X,Y)
* locations of the motion events, which caused the letter to flicker and move back and forth. The only solution I could
* find was to determine when the user had touched down on the letter, then process moves in the ACTION_MOVE
* associated with the mainLayout.
*/publicbooleanonTouch(View v, MotionEvent event) {
booleaneventConsumed=true;
intx= (int)event.getX();
inty= (int)event.getY();
intaction= event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
letterView.setImageResource(R.drawable.carkey1);
dragging = true;
eventConsumed = false;
}
} elseif (action == MotionEvent.ACTION_UP) {
if (dragging) {
emptyLetterView.getHitRect(hitRect);
if (hitRect.contains(x, y)) {
letterView.setImageResource(R.drawable.carkey3);
setSameAbsoluteLocation(letterView, emptyLetterView);
}
}
dragging = false;
eventConsumed = false;
} elseif (action == MotionEvent.ACTION_MOVE) {
if (v != letterView) {
if (dragging) {
setAbsoluteLocationCentered(letterView, x, y);
}
}
}
return eventConsumed;
}
privatevoidsetSameAbsoluteLocation(View v1, View v2) {
AbsoluteLayout.LayoutParamsalp2= (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
setAbsoluteLocation(v1, alp2.x, alp2.y);
}
privatevoidsetAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
privatevoidsetAbsoluteLocation(View v, int x, int y) {
AbsoluteLayout.LayoutParamsalp= (AbsoluteLayout.LayoutParams) v.getLayoutParams();
alp.x = x;
alp.y = y;
v.setLayoutParams(alp);
}
}
xml
<xmlversion="1.0"encoding="utf-8"?><AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/mainLayout"android:layout_width="fill_parent"android:layout_height="fill_parent" ><ImageViewandroid:id="@+id/emptyLetterView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/carkey2" ></ImageView><ImageViewandroid:id="@+id/letterView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="200dp"android:layout_y="450dp"android:src="@drawable/carkey1" ></ImageView></AbsoluteLayout>
Post a Comment for "How To Drag Images From One Imageview And Drop To Another Image View."