使用滑动手势移动对象 up/down
Move object up/down with swipe gesture
我试图在 max_height
和 min_height
值之间移动对象,我找到了一段代码并尝试对其进行调整,但是对象 (CardView
) 在屏幕的整个高度移动,当我尝试移动对象时,对象会在移动前重新出现在另一个位置,我不知道如何适应我的需要,有什么想法吗?
public interface OnLayoutCloseListener {
void OnLayoutClosed();
}
enum Direction {
UP_DOWN,
LEFT_RIGHT,
NONE
}
private Direction direction = Direction.NONE;
private int previousFingerPositionY;
private int previousFingerPositionX;
private int baseLayoutPosition;
private boolean isScrollingUp;
private boolean isLocked = false;
private OnLayoutCloseListener listener;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 50 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!isLocked) {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
baseLayoutPosition = (int) this.getY();
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (direction == Direction.NONE) {
if (Math.abs(diffX) > Math.abs(diffY)) {
direction = Direction.LEFT_RIGHT;
} else if (Math.abs(diffX) < Math.abs(diffY)) {
direction = Direction.UP_DOWN;
} else {
direction = Direction.NONE;
}
}
if (direction == Direction.UP_DOWN) {
isScrollingUp = diffY <= 0;
this.setY(baseLayoutPosition + diffY);
requestLayout();
return true;
}
} else if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
if (direction == Direction.UP_DOWN) {
if (isScrollingUp) {
//Calculates height according to my needs
int max_height = height - (card.getHeight() + toolbar.getHeight());
if (Math.abs(this.getY()) > max_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
} else {
//Calculates height according to my needs
int min_height = height - ((int)(toolbar.getHeight() * 1.7));
if (Math.abs(this.getY()) > min_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
}
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), 0);
positionAnimator.setDuration(0);
positionAnimator.start();
direction = Direction.NONE;
return true;
}
direction = Direction.NONE;
}
return true;
}
return false;
}
public void setOnLayoutCloseListener(OnLayoutCloseListener closeListener) {
this.listener = closeListener;
}
public void lock() {
isLocked = true;
}
public void unLock() {
isLocked = false;
}
更新解决方案:
在卡的任意实例重置LayoutParam
:
card.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
比在 min_height
和 max_height
之间使用此代码滚动视图
private int previousFingerPositionY;
private int previousFingerPositionX;
int min_height = 500;
int max_height = 100;
int pressedy;
int viewMariginY;
private boolean isLocked = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 25 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int currenty=(int) event.getRawY();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) card.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
pressedy=currenty;
viewMariginY=layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE :
int diffy=currenty-pressedy;
int marginy=viewMariginY+diffy;
layoutParams.topMargin=marginy;
if(marginy >= max_height && marginy <= min_height)
{
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy);
positionAnimator.setDuration(0);
positionAnimator.start();
}
break;
case MotionEvent.ACTION_UP :
int diffy2=currenty-pressedy;
int marginy2=viewMariginY+diffy2;
layoutParams.topMargin=marginy2;
if(marginy2 >= max_height && marginy2 <= min_height)
{
ObjectAnimator positionAnimator1 = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy2);
positionAnimator1.setDuration(0);
positionAnimator1.start();
}
break;
}
return true;
}
这个动画:
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), 0);
将垂直轴上的视图从 this.getY()
y 位置移动到 0(屏幕顶部)。
我看到您设置了一些界限 max_height
和 min_height
,但您没有以任何方式使用它们。
我不确定你的要求是什么,但你可以这样做:
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), (Math.abs(this.getY() - min_height) < Math.abs(this.getY() - max_height))?min_height:max_height);
这将做的是根据最近的对象将对象移动到 min_height
或 max_height
。
视图似乎也被此调用 this.setY(baseLayoutPosition + diffY); requestLayout();
动画化,您必须确保 baseLayoutPosition + diffY
在范围内,例如:
int amount = baseLayoutPosition + diffY;
this.setY(Math.min(max_height, Math.max(min_height, amount)));
int pressedx,pressedy;
int viewMariginX,viewMariginY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int currentx=(int) event.getRawX();
int currenty=(int) event.getRawY();
//get Layout Param of your cardView
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
pressedx=currentx;
pressedy=currenty;
viewMariginX=layoutParams.leftMargin;
viewMariginY=layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE :
int diffx=currentx-pressedx;
int diffy=currenty-pressedy;
int marginx=viewMariginX+diffx;
int marginy=viewMariginY+diffy;
layoutParams.leftMargin=marginx;
layoutParams.topMargin=marginy;
v.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP :
int diffx2=currentx-pressedx;
int diffy2=currenty-pressedy;
int marginx2=viewMariginX+diffx2;
int marginy2=viewMariginY+diffy2;
layoutParams.leftMargin=marginx2;
layoutParams.topMargin=marginy2;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
您的参考与我几天前所做的类似。
取两个位置的差,从左到上添加到当前视图边距。
您可以通过保存这些保证金值来保留视图的位置。
注意:您必须注意您的 MAX 和 MIN 范围
希望对您有所帮助...
更新:
1)
随心所欲地在你身上附加 onTouchListners
cardview.setOnTouchListener(这个);
cardview1.setOnTouchListener(这个);
OnTouch(View v, MotionEvent事件)
在将触摸事件分派到视图时调用。这使听众有机会在目标视图之前做出响应。
指定者:OnTouchListener 中的 onTouch(...)
参数:
v :触摸事件被发送到的视图。
event :包含有关事件的完整信息的 MotionEvent 对象。
通过文档。
将 onTouch 中的卡片视图更改为 v
根据你的问题
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(v, "y", this.getY(), marginy);
positionAnimator.setDuration(0);
positionAnimator.start();
以相同的方法更改更多引用。
2) 设置边界的问题很简单改变位置前的条件检查。
抱歉解释不当。
我试图在 max_height
和 min_height
值之间移动对象,我找到了一段代码并尝试对其进行调整,但是对象 (CardView
) 在屏幕的整个高度移动,当我尝试移动对象时,对象会在移动前重新出现在另一个位置,我不知道如何适应我的需要,有什么想法吗?
public interface OnLayoutCloseListener {
void OnLayoutClosed();
}
enum Direction {
UP_DOWN,
LEFT_RIGHT,
NONE
}
private Direction direction = Direction.NONE;
private int previousFingerPositionY;
private int previousFingerPositionX;
private int baseLayoutPosition;
private boolean isScrollingUp;
private boolean isLocked = false;
private OnLayoutCloseListener listener;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 50 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!isLocked) {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
baseLayoutPosition = (int) this.getY();
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (direction == Direction.NONE) {
if (Math.abs(diffX) > Math.abs(diffY)) {
direction = Direction.LEFT_RIGHT;
} else if (Math.abs(diffX) < Math.abs(diffY)) {
direction = Direction.UP_DOWN;
} else {
direction = Direction.NONE;
}
}
if (direction == Direction.UP_DOWN) {
isScrollingUp = diffY <= 0;
this.setY(baseLayoutPosition + diffY);
requestLayout();
return true;
}
} else if (ev.getActionMasked() == MotionEvent.ACTION_UP) {
if (direction == Direction.UP_DOWN) {
if (isScrollingUp) {
//Calculates height according to my needs
int max_height = height - (card.getHeight() + toolbar.getHeight());
if (Math.abs(this.getY()) > max_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
} else {
//Calculates height according to my needs
int min_height = height - ((int)(toolbar.getHeight() * 1.7));
if (Math.abs(this.getY()) > min_height) {
if (listener != null) {
listener.OnLayoutClosed();
}
}
}
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), 0);
positionAnimator.setDuration(0);
positionAnimator.start();
direction = Direction.NONE;
return true;
}
direction = Direction.NONE;
}
return true;
}
return false;
}
public void setOnLayoutCloseListener(OnLayoutCloseListener closeListener) {
this.listener = closeListener;
}
public void lock() {
isLocked = true;
}
public void unLock() {
isLocked = false;
}
更新解决方案:
在卡的任意实例重置LayoutParam
:
card.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
比在 min_height
和 max_height
private int previousFingerPositionY;
private int previousFingerPositionX;
int min_height = 500;
int max_height = 100;
int pressedy;
int viewMariginY;
private boolean isLocked = false;
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isLocked) {
return false;
} else {
final int y = (int) ev.getRawY();
final int x = (int) ev.getRawX();
if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
previousFingerPositionX = x;
previousFingerPositionY = y;
} else if (ev.getActionMasked() == MotionEvent.ACTION_MOVE) {
int diffY = y - previousFingerPositionY;
int diffX = x - previousFingerPositionX;
if (Math.abs(diffX) + 25 < Math.abs(diffY)) {
return true;
}
}
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int currenty=(int) event.getRawY();
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) card.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
pressedy=currenty;
viewMariginY=layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE :
int diffy=currenty-pressedy;
int marginy=viewMariginY+diffy;
layoutParams.topMargin=marginy;
if(marginy >= max_height && marginy <= min_height)
{
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy);
positionAnimator.setDuration(0);
positionAnimator.start();
}
break;
case MotionEvent.ACTION_UP :
int diffy2=currenty-pressedy;
int marginy2=viewMariginY+diffy2;
layoutParams.topMargin=marginy2;
if(marginy2 >= max_height && marginy2 <= min_height)
{
ObjectAnimator positionAnimator1 = ObjectAnimator.ofFloat(card, "y", this.getY(), marginy2);
positionAnimator1.setDuration(0);
positionAnimator1.start();
}
break;
}
return true;
}
这个动画:
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), 0);
将垂直轴上的视图从 this.getY()
y 位置移动到 0(屏幕顶部)。
我看到您设置了一些界限 max_height
和 min_height
,但您没有以任何方式使用它们。
我不确定你的要求是什么,但你可以这样做:
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(card, "y", this.getY(), (Math.abs(this.getY() - min_height) < Math.abs(this.getY() - max_height))?min_height:max_height);
这将做的是根据最近的对象将对象移动到 min_height
或 max_height
。
视图似乎也被此调用 this.setY(baseLayoutPosition + diffY); requestLayout();
动画化,您必须确保 baseLayoutPosition + diffY
在范围内,例如:
int amount = baseLayoutPosition + diffY;
this.setY(Math.min(max_height, Math.max(min_height, amount)));
int pressedx,pressedy;
int viewMariginX,viewMariginY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int currentx=(int) event.getRawX();
int currenty=(int) event.getRawY();
//get Layout Param of your cardView
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
pressedx=currentx;
pressedy=currenty;
viewMariginX=layoutParams.leftMargin;
viewMariginY=layoutParams.topMargin;
break;
case MotionEvent.ACTION_MOVE :
int diffx=currentx-pressedx;
int diffy=currenty-pressedy;
int marginx=viewMariginX+diffx;
int marginy=viewMariginY+diffy;
layoutParams.leftMargin=marginx;
layoutParams.topMargin=marginy;
v.setLayoutParams(layoutParams);
break;
case MotionEvent.ACTION_UP :
int diffx2=currentx-pressedx;
int diffy2=currenty-pressedy;
int marginx2=viewMariginX+diffx2;
int marginy2=viewMariginY+diffy2;
layoutParams.leftMargin=marginx2;
layoutParams.topMargin=marginy2;
v.setLayoutParams(layoutParams);
break;
}
return true;
}
您的参考与我几天前所做的类似。
取两个位置的差,从左到上添加到当前视图边距。
您可以通过保存这些保证金值来保留视图的位置。
注意:您必须注意您的 MAX 和 MIN 范围
希望对您有所帮助...
更新: 1) 随心所欲地在你身上附加 onTouchListners
cardview.setOnTouchListener(这个); cardview1.setOnTouchListener(这个);
OnTouch(View v, MotionEvent事件) 在将触摸事件分派到视图时调用。这使听众有机会在目标视图之前做出响应。
指定者:OnTouchListener 中的 onTouch(...) 参数: v :触摸事件被发送到的视图。 event :包含有关事件的完整信息的 MotionEvent 对象。 通过文档。
将 onTouch 中的卡片视图更改为 v
根据你的问题
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) v.getLayoutParams();
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(v, "y", this.getY(), marginy); positionAnimator.setDuration(0); positionAnimator.start();
以相同的方法更改更多引用。
2) 设置边界的问题很简单改变位置前的条件检查。
抱歉解释不当。