Android Drag and drop images on the Screen?
Write Below Code into your Activity File.
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
tv1 = (ImageView)findViewById(R.id.image);
tv1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
tv1.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
tv2 = (ImageView)findViewById(R.id.image1);
tv2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
tv2.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});
XML File:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="50sp" android:layout_height="50sp"
android:id="@+id/image" android:src="@drawable/image">
</ImageView>
<ImageView android:layout_y="30dip" android:layout_x="118dip"
android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
android:src="@drawable/image1">
</ImageView>
</RelativeLayout>
Drag & Drop image in Screen size of device using android
finally After lots of research i found the solution with this awesome repo. use this library :)
Floating button in github
Android: Drag ImageView Off Screen
The view shrinks because at some point you change the view's width/height, i.e. say getRight() - getLeft()
is forced to decrease because there is not enough space on the screen.
Anyway, setting view's coordinates directly is not something you should do. Instead implement your custom ViewGroup/layout:
- Override
onLayout()
- Iterate through children: use your custom logic to determine top and left coordinate for each child. In your case it would be using touch events to calculate the coordinates during dragging. For the right and bottom coordinates you can add the measured width/height of the child.
- On each child call
child.layout(left, top, right, bottom)
For an example see the code of FrameLayout at line 513 and the docs for onLayout().
Android Drag and Drop. Positioning image after dragging
Replace
view.setX(X);
view.setY(Y);
with
view.setX(X-(view.getWidth()/2));
view.setY(Y-(view.getHeight()/2));
When dragging an Imageview goes outside the screen
After spend more time on it, finally find the best solution.
Modify my OnTouch listerner section as :
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
@SuppressLint("NewApi") @Override
public boolean onTouch(View view, MotionEvent event) {
float newX, newY;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
newX = event.getRawX() + dX;
newY = event.getRawY() + dY;
// check if the view out of screen
if ((newX <= 0 || newX >= screenWidth-view.getWidth()) || (newY <= 0 || newY >= screenHight-view.getHeight()))
{
lastAction = MotionEvent.ACTION_MOVE;
break;
}
view.setX(newX);
view.setY(newY);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
break;
default:
return false;
}
return true;
}
It's working perfectly :)
Android Drag and Drop (not able to shuffle images)
The reason why you're not able to shuffle your images and merge the views it's because you're not handling the drop events correctly.
Take a look at this Drag and Drop open source example:
https://github.com/theredsunrise/AndroidCoolDragAndDropGridView
Related Topics
"Gps" Location Provider Requires Access_Fine_Location Permission for Android 6.0
Image Share Intent Works for Gmail But Crashes Fb and Twitter
What's the Maximum Size for an Android Shared Preference Value
Manipulating Data on Webs in Android
App-Release-Unsigned.Apk Is Not Signed
Android Imageview Adjusting Parent's Height and Fitting Width
How to Use the Android Keystore to Securely Store Arbitrary Strings
How to Get App Install Time from Android
Play Sound Using Soundpool Example
Javadoc in Eclipse Failing to Recognize Packages
How to Change Style of a Default Edittext
What Is a Maximum Size of SQLite Database on Android
Android L (API 21) - Java.Lang.Illegalargumentexception: Service Intent Must Be Explicit
Android Phone Orientation Overview Including Compass
Do I Need 14 Different Layouts to Support All Android Devices