我这一次讲使用scroll实现弹性滑动,我不会只有一个例子就说完,因为写文章的时候我也在学习,我分几次讲完吧。
首先上一段代码,
private void smoothScrollByScroller(int dy){ mScroller.startScroll(0,dy,0,dy*-1,1000); invalidate(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } }
这段代码是实现弹性滑动的核心,第一个函数指的是缓慢滑动的意思,但是却没有这个滑动的实际功能。
startScroll这函数的五个参数指的是起点x坐标,起点y坐标,x位移量,y位移量,这段滑动的时间。这个函数的内部是不断计算在滑动时间里x和y坐标应该是什么值,然后因为invalidate会调用computeScroll,这个computeScrollOffset函数是判断当前滑动是否结束,如果没有结束通过getCurrX和getCurry获得startScroll函数计算的值,在使用scrollTo滑动相应的位置,因为startScroll会运算很多次,也就是将滑动时间分成很多段,相应的坐标也都算出来,跟着给scrollTo去实现滑动。
这很像是ValueAmition,将时间分成很多段,然后计算相应的值,同时分很多次去实现。
我贴一个类似QQ消息列表的常见的弹性滑动,这里下拉是没有刷新的,
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } public final class PullView extends ViewGroup { private int mLastY; private Context mContext; private Scroller mScroller; //子View的个数 private int mChildCount; public PullView(Context context){ this(context,null); } public PullView(Context context, AttributeSet attributeSet){ super(context,attributeSet); mContext=context; initView(); } private void initView(){ mScroller=new Scroller(mContext); } @Override public boolean onTouchEvent(MotionEvent event) { int y=(int)event.getY(); switch (event.getAction()){ //手指按下时,初始化按下位置的X,Y位置值 case MotionEvent.ACTION_DOWN: mLastY=y; break; //计算滑动的偏移量,产生滑动效果 case MotionEvent.ACTION_MOVE: //手指向下滑动delayY>0,向上滑动delayY<0 int delayY=y-mLastY; delayY=delayY*-1; scrollBy(0,delayY); break; case MotionEvent.ACTION_UP: /** * scrollY是指:View的上边缘和View内容的上边缘(其实就是第一个ChildView的上边缘)的距离 * scrollY=上边缘-View内容上边缘,scrollTo/By方法滑动的知识View的内容 * 往下滑动scrollY是负值 */ int scrollY=getScrollY(); smoothScrollByScroller(scrollY); break; } mLastY=y; return true; } /** * 执行滑动效果 * 使用scroller实现 * @param dy */ private void smoothScrollByScroller(int dy){ mScroller.startScroll(0,dy,0,dy*-1,1000); invalidate(); } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } /** * 重新计算子View的高度和宽度 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measuredWidth; int measureHeight; mChildCount = getChildCount(); //测量子View measureChildren(widthMeasureSpec, heightMeasureSpec); int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec); int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec); int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec); int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec); //获取横向的padding值 int paddingLeft=getPaddingLeft(); int paddingRight=getPaddingRight(); final View childView = getChildAt(0); /** * 如果子View的数量是0,就读取LayoutParams中数据 * 否则就对子View进行测量 * 此处主要是针对wrap_content这种模式进行处理,因为默认情况下 * wrap_content等于match_parent */ if (mChildCount == 0) { ViewGroup.LayoutParams layoutParams=getLayoutParams(); if(layoutParams!=null){ setMeasuredDimension(layoutParams.width,layoutParams.height); }else { setMeasuredDimension(0, 0); } } else if (heightSpaceMode == MeasureSpec.AT_MOST && widthSpaceMode == MeasureSpec.AT_MOST) { measuredWidth = childView.getMeasuredWidth() * mChildCount; measureHeight = getChildMaxHeight(); //将两侧的padding值加上去 measuredWidth=paddingLeft+measuredWidth+paddingRight; setMeasuredDimension(measuredWidth, measureHeight); } else if (heightSpaceMode == MeasureSpec.AT_MOST) { measureHeight = getChildMaxHeight(); setMeasuredDimension(widthSpaceSize, measureHeight); } else if (widthSpaceMode == MeasureSpec.AT_MOST) { measuredWidth = childView.getMeasuredWidth() * mChildCount; measuredWidth=paddingLeft+measuredWidth+paddingRight; setMeasuredDimension(measuredWidth, heightSpaceSize); } } /** * 获取子View中最大高度 * @return */ private int getChildMaxHeight(){ int maxHeight=0; for (int i = 0; i < mChildCount; i++) { View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { int height = childView.getMeasuredHeight(); if(height>maxHeight){ maxHeight=height; } } } return maxHeight; } /** * 设置子View的布局 * @param changed * @param l * @param t * @param r * @param b */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childLeft = 0; for (int i = 0; i < mChildCount; i++) { View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { int childWidth = childView.getMeasuredWidth(); childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight()); childLeft += childWidth; } } } } <android.com.listfragment.PullView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="1500dp" android:background="#806363"></LinearLayout> </android.com.listfragment.PullView>
这里的ViewGroup的绘画和测量我就不多说,我就说一下它获取函数,计算坐标的一些事。
它在手指按下时记录y坐标,在手指移动时,跟着移动子View,在手指抬起时,使用弹性滑动的函数smoothScrollByScroller。
大家会发现为什么一些计算出的坐标要加负号,因为在我们人眼里,我们下拉y坐标的位移量是正的,但是在系统认为这个值是负的,原因我太菜不知道,知道的求大神评论留言告诉。
下一次写一个随手指弹性滑动的例子。
以上这篇Android Scroll实现弹性滑动_列表下拉弹性滑动的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持小牛知识库。
本文向大家介绍Jquery实现弹性滑块滑动选择数值插件,包括了Jquery实现弹性滑块滑动选择数值插件的使用技巧和注意事项,需要的朋友参考一下 做了一个类似于阿里云弹性计算中的滑块效果,可以自由滑动滑块,并且计算刻度。基于jQuery实现,小伙伴们不要忘记载入jquery哦 CSS HTML JS 演示图: 演示地址:http://demo.jb51.net/js/2015/jquery-txhk
本文向大家介绍Android使用Scroller实现弹性滑动效果,包括了Android使用Scroller实现弹性滑动效果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Android使用Scroller实现弹性滑动展示的具体代码,供大家参考,具体内容如下 scrollTo、scrollBy View内部为了实现滑动提供了这两个方法,但是使用这两个方法滑动的效果是瞬间的不够平滑,如何
滑动弹框。支持四个触控区域,十二个动画方向。 Usage 全部引入 import { SlideModal } from 'beeshell'; 按需引入 import SlideModal from 'beeshell/dist/components/SlideModal'; Examples 基础 四个触控区域 main 区域是内容所在区域,带有遮罩,无法点击遮罩下面元素。 从 main
定义 自定义方向、宽、高、对齐方式可拖出的弹层组件。 图片展示 代码演示 import SwipeMove from 'pile/dist/components/swipemove' <SwipeMove height="100%" width="100%" direction="bottom" align={0}> <div className="move-content"> <h2>弹层
本文向大家介绍Android仿IOS上拉下拉弹性效果的实例代码,包括了Android仿IOS上拉下拉弹性效果的实例代码的使用技巧和注意事项,需要的朋友参考一下 用过iphone的朋友相信都体验过页面上拉下拉有一个弹性的效果,使用起来用户体验很好;Android并没有给我们封装这样一个效果,我们来看下在Android里如何实现这个效果。先看效果,感觉有些时候还是蛮实用的。 思路:其实原理很简单,实现
本文向大家介绍Android中实现水平滑动(横向滑动)ListView示例,包括了Android中实现水平滑动(横向滑动)ListView示例的使用技巧和注意事项,需要的朋友参考一下 水平的ListView-HorizontalListView的使用 Android中ListView默认的是竖直方向的滑动,由于项目的需求,需要ListView是水平滑动的。有很多的方式可以实现,但是比较好的一种方式