拿起手机,打开QQ,随便找个页面再随便侧滑一下,发现当前Activity执行关闭了,当放慢侧滑速率时不会触发Activity返回效果。
SmartSwipe框架的StayConsumer
可以根据侧滑速率实现类似手机QQ的返回效果。
StayConsumer
的代码如下:
/**
* Swipe to do something, the contentView do not move,
* when swipe released, determine whether the velocity and direction is enough, do some business if success
* @author billy.qi
*/
public class StayConsumer extends SwipeConsumer {
private int mMinVelocity = 1000;
public StayConsumer() {
setOpenDistance(Integer.MAX_VALUE)
.setMaxSettleDuration(0);
}
@Override
protected void onDisplayDistanceChanged(int distanceXToDisplay, int distanceYToDisplay, int dx, int dy) {
//do nothing
}
@Override
public void onSwipeReleased(float xVelocity, float yVelocity) {
if (Math.abs(xVelocity) > Math.abs(yVelocity)) {
if (mDirection == DIRECTION_LEFT && xVelocity >= mMinVelocity || (mDirection == DIRECTION_RIGHT && xVelocity <= -mMinVelocity)) {
mCurSwipeDistanceX = getSwipeOpenDistance();
mProgress = 1;
}
} else {
if (mDirection == DIRECTION_TOP && yVelocity >= mMinVelocity || (mDirection == DIRECTION_BOTTOM && yVelocity <= -mMinVelocity)) {
mCurSwipeDistanceY = getSwipeOpenDistance();
mProgress = 1;
}
}
super.onSwipeReleased(xVelocity, yVelocity);
}
public int getMinVelocity() {
return mMinVelocity;
}
public StayConsumer setMinVelocity(int minVelocity) {
if (minVelocity > 0) {
this.mMinVelocity = minVelocity;
}
return this;
}
}
mMinVelocity为最小速度,当滑动速度超过这个最小值时,mProgress从0变成1,也就是说从默认的关闭状态立即切换到打开状态。
换一种说法是:主体contentView在侧滑过程中保持不动,侧滑结束时按照手势滑动的方向和速率来确定是否开启,若开启将回调[SwipeListener]的onSwipeOpen方法。
效果如下:

从效果图中看不到侧滑的效果,因为StayConsumer
是没有侧滑效果的,它只有计算侧滑方向和速率的实现。
代码实现如下:
SmartSwipe.wrap(this)
.addConsumer(new StayConsumer())
.enableAllDirections()
.addListener(new SimpleSwipeListener(){
@Override
public void onSwipeOpened(SmartSwipeWrapper wrapper, SwipeConsumer consumer, int direction) {
//关闭当前界面
ImageViewerActivity.this.finish();
}
});
[本章完...]
网友评论