In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Android how to customize View to achieve vertical sliding springback effect". In daily operation, I believe many people have doubts about how to customize Android View to achieve vertical sliding springback effect. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for everyone to answer "how Android customize View to achieve vertical sliding springback effect". Next, please follow the editor to study!
1. Key code
Public class UniversalBounceView extends FrameLayout implements IPull {private static final String TAG = "UniversalBounceView"; / / default. Private static final int SCROLL_DURATION = 200; private static final float SCROLL_FRACTION = 0.4f; private static final int VIEW_TYPE_NORMAL = 0; private static final int VIEW_TYPE_ABSLISTVIEW = 1; private static final int VIEW_TYPE_SCROLLVIEW = 2; private static float VIEW_SCROLL_MAX = 720; private int viewHeight; private AbsListView alv; private OnBounceStateListener onBounceStateListener; private View child; private Scroller scroller; private boolean pullEnabled = true; private boolean pullPaused; private int touchSlop = 8 Private int mPointerId; private float downY, lastDownY, tmpY; private int lastPointerIndex; private float moveDiffY; private boolean isNotJustInClickMode; private int moveDelta; private int viewType = VIEW_TYPE_NORMAL; public UniversalBounceView (Context context) {super (context); init (context);} public UniversalBounceView (Context context, AttributeSet attrs) {super (context, attrs); init (context) } public UniversalBounceView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context);} private void init (Context context) {scroller = new Scroller (context, new CustomDecInterpolator ()); touchSlop = (int) (ViewConfiguration.get (context). GetScaledTouchSlop () * 1.5);} class CustomDecInterpolator extends DecelerateInterpolator {public CustomDecInterpolator () {super () } public CustomDecInterpolator (float factor) {super (factor);} public CustomDecInterpolator (Context context, AttributeSet attrs) {super (context, attrs);} @ Override public float getInterpolation (float input) {return (float) Math.pow (input, 6.012) } private void checkCld () {int cnt = getChildCount (); if (1 VIEW_SCROLL_MAX) {return true;} if ((canStart & & total)
< 0) || (canEnd && total >0)) {if (moveDelta
< touchSlop) { moveDelta += Math.abs(moveDiffY); } else { isNotJustInClickMode = true; } if (onBounceStateListener != null) { onBounceStateListener.onBounce(); } scrollBy(0, (int) -moveDiffY); return true; }// else if ((total >0 & & canStart) | | (total
< 0 && canEnd)) {// if (moveDelta < touchSlop) {// moveDelta += Math.abs(moveDiffY);// } else {// isNotJustInClickMode = true;// }// scrollBy(0, -scroll);// return true;// } break; case MotionEvent.ACTION_POINTER_UP: handlePointerUp(ev, 1); break; default: break; } return super.dispatchTouchEvent(ev); } private boolean canScrollInternal(int scroll, boolean canStart, boolean canEnd) { boolean result = false; if ((child instanceof RecyclerView) || (child instanceof AbsListView) || child instanceof ScrollView) { viewType = VIEW_TYPE_ABSLISTVIEW; result = canStart && canEnd; } else if (child instanceof ScrollView || child instanceof NestedScrollView) { viewType = VIEW_TYPE_SCROLLVIEW; } else { return false; } if (result) { isNotJustInClickMode = true; if (moveDelta < touchSlop) { moveDelta += Math.abs(moveDiffY); return true; } return false; } if (((scroll == 0 && canStart && moveDiffY < 0) || (scroll == 0 && canEnd && moveDiffY >0) | | (! canStart & &! canEnd)) {return true;} if (moveDelta
< touchSlop) { moveDelta += Math.abs(moveDiffY); return true; } else { isNotJustInClickMode = true; } return false; } private void handleInternal() { } private void handlePointerUp(MotionEvent event, int type) { int pointerIndexLeave = event.getActionIndex(); int pointerIdLeave = event.getPointerId(pointerIndexLeave); if (mPointerId == pointerIdLeave) { int reIndex = pointerIndexLeave == 0 ? 1 : 0; mPointerId = event.getPointerId(reIndex); // 调整触摸位置,防止出现跳动 downY = event.getY(reIndex); } } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } private void smoothScrollTo(int value) { int scroll = getScrollY(); scroller.startScroll(0, scroll, 0, value - scroll, SCROLL_DURATION); postInvalidate(); } @Override public void computeScroll() { super.computeScroll(); if (!pullPaused && scroller.computeScrollOffset()) { scrollTo(scroller.getCurrX(), scroller.getCurrY()); postInvalidate(); } } private float getScrollFraction() { float ratio = Math.abs(getScrollY()) / VIEW_SCROLL_MAX; ratio = ratio < 1 ? ratio : 1; float fraction = (float) (-2 * Math.cos((ratio + 1) * Math.PI) / 5.0f) + 0.1f; return fraction < 0.10f ? 0.10f : fraction; } @Override public boolean isPullEnable() { return pullEnabled; } @Override public boolean isCanPullStart() { if (child instanceof RecyclerView) { RecyclerView recyclerView = (RecyclerView) child; return !recyclerView.canScrollVertically(-1); } if (child instanceof AbsListView) { AbsListView lv = (AbsListView) child; return !lv.canScrollVertically(-1); } if (child instanceof RelativeLayout || child instanceof FrameLayout || child instanceof LinearLayout || child instanceof WebView || child instanceof View) { return child.getScrollY() == 0; } return false; } @Override public boolean isCanPullEnd() { if (child instanceof RecyclerView) { RecyclerView recyclerView = (RecyclerView) child; return !recyclerView.canScrollVertically(1); } if (child instanceof AbsListView) { AbsListView lv = (AbsListView) child; int first = lv.getFirstVisiblePosition(); int last = lv.getLastVisiblePosition(); View view = lv.getChildAt(last - first); if (null == view) { return false; } else { return (lv.getCount() - 1 == last) && (view.getBottom() = v.getHeight() - child.getHeight(); } } if (child instanceof NestedScrollView) { View v = ((NestedScrollView) child).getChildAt(0); if (null == v) { return true; } else { return child.getScrollY() >= v.getHeight ()-child.getHeight ();}} if (child instanceof WebView) {return ((WebView) child). GetContentHeight () * ((WebView) child) .getScale ()-((WebView) child) .getHeight () + ((WebView) child) .getScrollY ())
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.