Comments (2)
Hi @cryinrain69 , you can use the updated 0.3.0 version of this library
It adds DraggableUtils
class so it can be used on java class
See the updated usage / example on Readme
Thanks for the feedback and your interest in using this library 🙏
from draggableview.
float widgetInitialX = 0f;
float widgetDX = 0f;
float widgetInitialY = 0f;
float widgetDY = 0f;
public void makeDraggable(View view, Draggable.STICKY stickyAxis, boolean animated, DraggableListener mDraggableListener) {
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
View viewParent = (View) v.getParent();
float parentHeight = viewParent.getHeight();
float parentWidth = viewParent.getWidth();
float xMax = parentWidth - v.getWidth();
float xMiddle = parentWidth / 2;
float yMax = parentHeight - v.getHeight();
float yMiddle = parentHeight / 2;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
widgetDX = v.getX() - event.getRawX();
widgetDY = v.getY() - event.getRawY();
widgetInitialX = v.getX();
widgetInitialY = v.getY();
break;
case MotionEvent.ACTION_MOVE:
float newX = event.getRawX() + widgetDX;
newX = Math.max(0F, newX);
newX = Math.min(xMax, newX);
v.setX(newX);
float newY = event.getRawY() + widgetDY;
newY = Math.max(0F, newY);
newY = Math.min(yMax, newY);
v.setY(newY);
mDraggableListener.onPositionChanged(v);
break;
case MotionEvent.ACTION_UP:
switch (stickyAxis) {
case AXIS_X:
if (event.getRawX() >= xMiddle) {
if (animated)
v.animate().x(xMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setX(xMax);
} else {
if (animated)
v.animate().x(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setX(0F);
}
break;
case AXIS_Y:
if (event.getRawY() >= yMiddle) {
if (animated)
v.animate().y(yMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setY(yMax);
} else {
if (animated)
v.animate().y(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setY(0F);
}
break;
case AXIS_XY:
if (event.getRawX() >= xMiddle) {
if (animated)
v.animate().x(xMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setX(xMax);
} else {
if (animated)
v.animate().x(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setX(0F);
}
if (event.getRawY() >= yMiddle) {
if (animated)
v.animate().y(yMax).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setY(yMax);
} else {
if (animated)
v.animate().y(0F).setDuration(DURATION_MILLIS).setUpdateListener(animation -> mDraggableListener.onPositionChanged(v)).start();
else
v.setY(0F);
}
break;
}
break;
default:
return false;
}
if (Math.abs(v.getX() - widgetInitialX) <= 16 && Math.abs(v.getY() - widgetInitialY) <= 16) {
v.performClick();
}
return true;
}
});
}
from draggableview.
Related Issues (14)
- Create helper to extend the behaviour to any view HOT 2
- How to make draggable webview using this ?
- Is there a function to resize the ImageView while on dragging? HOT 1
- How to drag over other items?
- OnLongClickListener not working HOT 1
- Unable to implement for CameraX PreviewView HOT 3
- OnPositionChangedListener HOT 1
- implementation in scroll view? HOT 2
- ClassCastException raise when cast ViewRootImpl cast to View HOT 6
- disable drag
- View not dragging when it was on bottomsheet. HOT 1
- how can i maintain view position while orientation change ?
- how to change its view position by passing value of x and y ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from draggableview.