[英]Swipe gesture is very slow,…how to speed up my Swipe Gesture in android?
[英]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;
}
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;
}
您的參考與我幾天前所做的相似。
它需要兩個位置的差值,並將它們從左和從頂部添加到當前視圖邊距。
您可以通過保存這些邊距值來保留View的Position。
注意 :您必須注意自己的最大和最小界限
希望對您有幫助...
更新 :1)將onTouchListners附加到您想要的任意多個卡片視圖上
cardview.setOnTouchListener(this); cardview1.setOnTouchListener(this);
OnTouch(View v,MotionEvent event)在將touch事件調度到視圖時調用。 這使聽眾有機會在目標視圖之前做出響應。
指定者:OnTouchListener中的onTouch(...)參數:v:已將touch事件調度到的視圖。 event:包含有關事件的完整信息的MotionEvent對象。 通過文檔。
在onTouch中將您的Cardview更改為v
從你的問題
FrameLayout.LayoutParams layoutParams =(FrameLayout.LayoutParams)v.getLayoutParams();
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(v,“ y”,this.getY(),marginy); positionAnimator.setDuration(0); positionAnimator.start();
用相同的方法更改其他參考。
2)設置邊界的問題很簡單,僅在更改位置之前進行條件檢查。
很抱歉,您的解釋不正確。
此動畫:
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();
該視圖似乎也可以動畫化this.setY(baseLayoutPosition + diffY); requestLayout();
this.setY(baseLayoutPosition + diffY); requestLayout();
,您必須確保baseLayoutPosition + diffY
在范圍之內,例如:
int amount = baseLayoutPosition + diffY;
this.setY(Math.min(max_height, Math.max(min_height, amount)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.