侧滑效果[第六篇]:侧滑框架SmartSwipe之弹性拉伸效果
2019-11-12 本文已影响0人
NoBugException
弹性拉伸,即侧滑时,视图有被拉伸的效果。
首先看一下弹性拉伸效果:
282.gif代码如下:
StretchConsumer.java
/**
* @author billy.qi
*/
public class StretchConsumer extends SwipeConsumer {
@Override
public void onDetachFromWrapper() {
super.onDetachFromWrapper();
View contentView = mWrapper.getContentView();
if (contentView != null) {
contentView.setScaleX(1);
contentView.setScaleY(1);
contentView.setTranslationX(0);
contentView.setTranslationY(0);
}
}
@Override
public void onDisplayDistanceChanged(int distanceXToDisplay, int distanceYToDisplay, int dx, int dy) {
View contentView = mWrapper.getContentView();
if (contentView != null) {
if (distanceXToDisplay >= 0 && isLeftEnable() || distanceXToDisplay <= 0 && isRightEnable()) {
contentView.setScaleX(1 + Math.abs((float) distanceXToDisplay) / mWidth);
contentView.setTranslationX(distanceXToDisplay / 2F);
}
if (distanceYToDisplay >= 0 && isTopEnable() || distanceYToDisplay <= 0 && isBottomEnable()) {
contentView.setScaleY(1 + Math.abs((float) distanceYToDisplay) / mHeight);
contentView.setTranslationY(distanceYToDisplay / 2F);
}
}
}
}
为Recycleview的Item添加横向拉伸效果
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext).inflate(R.layout.recycleview_item, parent, false);
ViewHolder holder = new ViewHolder(SmartSwipe.wrap(itemView).addConsumer(new StretchConsumer()).enableHorizontal().getWrapper());
return holder;
}
为Recycleview本身添加纵向拉伸效果
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableVertical();
有关SmartSwipe框架的弹性拉伸方向,有:
- 仅上方
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableTop();
-
仅下方
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableBottom();
-
仅左方
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableLeft();
-
仅右方
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableRight();
-
水平方向
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableHorizontal();
-
垂直方向
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableVertical();
-
所有方向
SmartSwipe.wrap(mRecyclerView).addConsumer(new StretchConsumer()).enableAllDirections();
[本章完...]